/// <summary> /// Initializes a new instance of the <see cref="FlutnetBridge"/> class /// specifying how platform code and Flutter code communicate. /// </summary> public FlutnetBridge(FlutterEngine engine, FlutnetBridgeMode mode) { // Create the named channel for communicating with Flutter module using asynchronous method calls // NOTE: This channel is used to RECEIVE messages/requests FROM Flutter _methodChannelIncoming = FlutterMethodChannel.Create("flutnetbridge.incoming", engine.BinaryMessenger); _methodChannelIncoming.SetMethodCallHandler(HandleMethodCall); // Create a second named channel for diagnostic use only. // This channel is used, for example, to test if Flutter module is running // embedded into a native Xamarin app or as a standalone app _methodChannelTest = FlutterMethodChannel.Create("flutnetbridge.support", engine.BinaryMessenger); _methodChannelTest.SetMethodCallHandler(HandleMethodCallTest); // Create the named channel for communicating with Flutter module using event streams // NOTE: This channel is used to SEND messages/notifications TO Flutter // An event channel is a specialized platform channel intended for the use case of exposing platform events to Flutter as a Dart stream. // The Flutter SDK currently has no support for the symmetrical case of exposing Dart streams to platform code, though that could be built, if the need arises. // see: https://medium.com/flutter/flutter-platform-channels-ce7f540a104e _streamHandler = new StreamHandler(this); _eventChannel = FlutterEventChannel.Create("flutnetbridge.outgoing", engine.BinaryMessenger); _eventChannel.SetStreamHandler(_streamHandler); Mode = mode; FlutnetRuntime.OnPlatformEvent += FlutnetRuntimeOnPlatformEvent; if (Mode == FlutnetBridgeMode.WebSocket) { _socketService = new FlutnetWebSocketService(); } }
/// <summary> /// Releases all resources used by this object. /// </summary> public void Dispose() { if (_disposed) { return; } FlutnetRuntime.OnPlatformEvent -= FlutnetRuntimeOnPlatformEvent; _methodChannelIncoming.Dispose(); _methodChannelTest.Dispose(); _eventChannel.Dispose(); _streamHandler.Dispose(); if (Mode == FlutnetBridgeMode.WebSocket) { _socketService?.Dispose(); _socketService = null; } _disposed = true; }