Exemplo n.º 1
0
        /// <summary>
        /// Registers a stream handler on this channel.
        /// </summary>
        /// <param name="handler">A <see cref="IEventStreamHandler"/>, or null to deregister.</param>
        public void SetStreamHandler(IEventStreamHandler handler)
        {
            BinaryMessageHandler binaryHandler = null;

            if (handler != null)
            {
                binaryHandler = new IncomingStreamRequestHandler(this, handler).OnMessage;
            }
            BinaryMessenger.SetMessageHandler(Name, binaryHandler);
        }
 public void SetMessageHandler(string channel, BinaryMessageHandler handler)
 {
     if (handler == null)
     {
         _handlers.Remove(channel);
         FlutterDesktopMessengerSetCallback(_messenger, channel, null, IntPtr.Zero);
         return;
     }
     _handlers[channel] = handler;
     FlutterDesktopMessengerSetCallback(_messenger, channel, _messageCallback, IntPtr.Zero);
 }
        async void BeginReceiveMessages()
        {
            isReceiving = true;

            while (isReceiving && client.State == WebSocketState.Open)
            {
                WebSocketReceiveResult receiveResult = null;

                using (var ms = new MemoryStream())
                {
                    do
                    {
                        receiveResult = await client.ReceiveAsync(clientBuffer, CancellationToken.None);

                        ms.Write(clientBuffer.Array, clientBuffer.Offset, receiveResult.Count);
                    }while (!receiveResult.EndOfMessage);

                    ms.Seek(0, SeekOrigin.Begin);

                    // close socket if requested by server, return
                    if (receiveResult.MessageType == WebSocketMessageType.Close)
                    {
                        await CloseSocket(receiveResult.CloseStatusDescription);

                        return;
                    }

                    if (receiveResult.MessageType == WebSocketMessageType.Binary)
                    {
                        BinaryMessageHandler?.OnMessageReceived(ms);
                    }
                    else if (receiveResult.MessageType == WebSocketMessageType.Text)
                    {
                        TextMessageHandler?.OnMessageReceived(ms);
                    }
                }
            }
        }