Example #1
0
        public Task ProcessWebSocketRequestAsync(System.Net.WebSockets.WebSocket socket)
        {
            _ = socket ?? throw new ArgumentNullException(nameof(socket));

            byte[] buffer = new byte[config.ReceiveLoopBufferSize];

            return(ProcessWebSocketRequestAsync(socket,
                                                () => WebSocketMessageReader.ReadMessageAsync(socket, buffer, config.MaxIncomingMessageSize,
                                                                                              CancellationToken.None)));
        }
Example #2
0
        public Task ProcessWebSocketRequestAsync(WebSocket socket)
        {
            if (socket == null)
            {
                throw new ArgumentNullException("socket");
            }
            byte[] buffer = new byte[config.ReceiveLoopBufferSize];

            return(ProcessWebSocketRequestAsync(socket, () => WebSocketMessageReader.ReadMessageAsync(socket, buffer, config.MaxIncomingMessageSize, CancellationToken.None)));
        }
Example #3
0
        public override async Task ReceiveAsync()
        {
            try
            {
                while (!token.IsCancellationRequested && IsConnected)
                {
                    ChannelReceivedEventArgs args = null;

                    WebSocketMessage message = await WebSocketMessageReader.ReadMessageAsync(client, new byte[config.ReceiveLoopBufferSize], config.MaxIncomingMessageSize, token);

                    if (message.Data != null)
                    {
                        if (message.MessageType == WebSocketMessageType.Binary)
                        {
                            args = new ChannelReceivedEventArgs(Id, message.Data as byte[]);
                        }
                        else if (message.MessageType == WebSocketMessageType.Text)
                        {
                            args = new ChannelReceivedEventArgs(Id, Encoding.UTF8.GetBytes(message.Data as string));
                        }
                        else
                        {
                            State = ChannelState.ClosedReceived;
                            break;
                        }

                        OnReceive?.Invoke(this, args);
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Web socket client channel {0} error {1}", Id, ex.Message);
            }

            await CloseAsync();
        }