public FlutterEventStream(EventChannel channel)
        {
            _channel = channel;

            _streamHandler = new FlutterStreamHandler();

            _channel.SetStreamHandler(_streamHandler);
        }
            public void Unregisters_Message_Handler()
            {
                var messenger = Substitute.For <IBinaryMessenger>();
                var channel   = new EventChannel(TEST_CHANNEL_NAME, StandardMethodCodec.Instance, messenger);

                channel.SetStreamHandler(null);

                messenger.Received().SetMessageHandler(Arg.Is <string>(x => x == TEST_CHANNEL_NAME), null);
            }
            public void Ensures_StreamHandler_Callbacks()
            {
                var messenger = Substitute.For <IBinaryMessenger>();
                var handler   = Substitute.For <IEventStreamHandler>();
                var channel   = new EventChannel(TEST_CHANNEL_NAME, StandardMethodCodec.Instance, messenger);

                byte[] listenCall = StandardMethodCodec.Instance.EncodeMethodCall(new MethodCall("listen", 1));
                byte[] cancelCall = StandardMethodCodec.Instance.EncodeMethodCall(new MethodCall("cancel", 1));
                messenger.When(x => x.SetMessageHandler(TEST_CHANNEL_NAME, Arg.Any <BinaryMessageHandler>()))
                .Do(x =>
                {
                    var binaryHandler = x[1] as BinaryMessageHandler;
                    binaryHandler(listenCall);
                    binaryHandler(cancelCall);
                });

                channel.SetStreamHandler(handler);

                handler.Received().OnListen(Arg.Is <int>(x => x == 1), Arg.Any <IEventSink>());
                handler.Received().OnCancel(Arg.Is <int>(x => x == 1));
            }
        /// <summary>
        /// Initializes a new instance of the <see cref="FlutnetBridge"/> class
        /// specifying how platform code and Flutter code communicate.
        /// </summary>
        public FlutnetBridge(FlutterEngine engine, Android.Content.Context context, 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     = new MethodChannel(engine.DartExecutor.BinaryMessenger, "flutnetbridge.incoming");
            _methodCallHandlerIncoming = new MethodCallHandler(HandleMethodCall);
            _methodChannelIncoming.SetMethodCallHandler(_methodCallHandlerIncoming);

            // 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     = new MethodChannel(engine.DartExecutor.BinaryMessenger, "flutnetbridge.support");
            _methodCallHandlerTest = new MethodCallHandler(HandleMethodCallTest);
            _methodChannelTest.SetMethodCallHandler(_methodCallHandlerTest);

            // 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  = new EventChannel(engine.DartExecutor.BinaryMessenger, "flutnetbridge.outgoing");
            _eventChannel.SetStreamHandler(_streamHandler);

            _context = context;
            Mode     = mode;

            FlutnetRuntime.OnPlatformEvent += FlutnetRuntimeOnPlatformEvent;

            if (Mode == FlutnetBridgeMode.WebSocket)
            {
                _context.StartService(new Android.Content.Intent(_context, typeof(FlutnetWebSocketService)));
            }
        }