Esempio n. 1
0
        /// <summary>
        /// Runs the receive loop for this session.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        protected virtual async Task RunReceiveAsync(CancellationToken cancellationToken)
        {
            var receiveStream = new MemoryStream();
            var buffer        = new byte[4096];

            while (Socket.State == WebSocketState.Open && !cancellationToken.IsCancellationRequested)
            {
                var receiveResult = await Socket.ReceiveAsync(new ArraySegment <byte>(buffer), cancellationToken);

                if (receiveResult.MessageType == WebSocketMessageType.Close)
                {
                    break;
                }

                if (receiveResult.MessageType == WebSocketMessageType.Text || receiveResult.MessageType == WebSocketMessageType.Binary)
                {
                    receiveStream.Write(buffer, 0, receiveResult.Count);

                    if (!receiveResult.EndOfMessage)
                    {
                        continue;
                    }

                    receiveStream.Position = 0;

                    if (receiveResult.MessageType == WebSocketMessageType.Text)
                    {
                        using (var sr = new StreamReader(receiveStream, Encoding.UTF8))
                        {
                            var json    = sr.ReadToEnd();
                            var message = JsonConverter.DeserializeObject <Message>(json);

                            if (message.Type == MessageType.Response)
                            {
                                ProcessResponse(Response.FromMessage(message));
                            }
                            else
                            {
                                await ProcessMessage(message);
                            }
                        }
                    }
                    else
                    {
                        var data = DataMessage.FromStream(receiveStream);

                        var bytes = data.Data;

                        if (data.CompressionMode == DataCompressionMode.Deflate)
                        {
                            using (var memoryStream = new MemoryStream())
                            {
                                using (var srcStream = new MemoryStream(bytes.Array, bytes.Offset, bytes.Count))
                                {
                                    using (var deflateStream = new DeflateStream(srcStream, CompressionMode.Decompress, false))
                                    {
                                        deflateStream.CopyTo(memoryStream);
                                    }
                                }

                                bytes = new ArraySegment <byte>(memoryStream.ToArray());
                            }
                        }

                        await ProcessData(data.ConnectionId, bytes);
                    }

                    receiveStream = new MemoryStream();
                }
            }
        }