コード例 #1
0
ファイル: SockJSWebSocket.cs プロジェクト: tmds/Tmds.SockJS
        public async override Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byte> buffer, CancellationToken cancellationToken)
        {
            while (true)
            {
                if (_receivedMessages != null)
                {
                    var head = _receivedMessages[0];
                    int length = head.Decode(buffer);
                    if (head.IsEmpty)
                    {
                        _receivedMessages.RemoveAt(0);
                        if (_receivedMessages.Count == 0)
                        {
                            _receivedMessages = null;
                        }
                    }
                    return new WebSocketReceiveResult(length, WebSocketMessageType.Text, true);
                }

                MemoryStream memoryStream = null;
                bool endOfMessage = false;
                var receiveBuffer = new byte[buffer.Count * 2];
                while (!endOfMessage)
                {
                    var receiveResult = await _webSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), cancellationToken);
                    if (receiveResult.MessageType == WebSocketMessageType.Text)
                    {
                        endOfMessage = receiveResult.EndOfMessage;
                        if (endOfMessage && (memoryStream == null))
                        {
                            memoryStream = new MemoryStream(receiveBuffer, 0, receiveResult.Count);
                        }
                        else
                        {
                            memoryStream = memoryStream ?? new MemoryStream();
                            memoryStream.Write(receiveBuffer, 0, receiveResult.Count);
                        }
                    }
                    else if (receiveResult.MessageType == WebSocketMessageType.Binary)
                    {
                        throw new NotSupportedException("SockJS: Binary messages are not supported");
                    }
                    else if (receiveResult.MessageType == WebSocketMessageType.Close)
                    {
                        return receiveResult;
                    }
                }
                if (memoryStream.Length == 0)
                {
                    continue;
                }

                memoryStream.Position = 0;
                var reader = new ReceiveMessageReader(memoryStream);
                try
                {
                    _receivedMessages = await reader.ReadMessages();
                }
                catch
                {
                    Abort();
                    throw;
                }
                if (_receivedMessages.Count == 0)
                {
                    _receivedMessages = null;
                }
            }
        }
コード例 #2
0
ファイル: SessionManager.cs プロジェクト: tmds/Tmds.SockJS
        private async Task HandleXhrSendAsync(HttpContext context, string sessionId)
        {
            AddSessionCookie(context);
            AddNoCacheHeader(context);
            AddCorsHeader(context);

            List<JsonString> messages;
            try
            {
                string mediaType = context.Request.ContentType?.Split(s_contentTypeSplitter)[0];
                if (!s_allowedXhrSendMediaTypes.Contains(mediaType))
                {
                    throw new Exception("Payload expected.");
                }

                var reader = new ReceiveMessageReader(context.Request.Body);
                messages = await reader.ReadMessages();
            }
            catch (Exception e)
            {
                context.Response.StatusCode = StatusCodes.Status500InternalServerError;
                await context.Response.WriteAsync(e.Message);
                return;
            }

            Session session = GetSession(sessionId);
            if (session == null || !session.IsAccepted)
            {
                await HandleNotFoundAsync(context);
                return;
            }

            session.ClientSend(messages);
            session.ExitSharedLock();

            context.Response.ContentType = "text/plain; charset=UTF-8";
            context.Response.StatusCode = StatusCodes.Status204NoContent;
            await ExposeNothingAsync();
        }