Beispiel #1
0
        private AppFunc WebSocketHandler(AppFunc next)
        {
            return((env) =>
            {
                var request = new OwinRequest(env);

                if (request.Path == "/Echo" && request.CanAccept)
                {
                    request.Accept(async(socket) =>
                    {
                        const int maxMessageSize = 1024;
                        byte[] receiveBuffer = new byte[maxMessageSize];

                        Console.WriteLine("WebSocket connection established.");

                        while (true)
                        {
                            ArraySegment <byte> buffer = new ArraySegment <byte>(receiveBuffer);
                            var received = await socket.ReceiveAsync(buffer, CancellationToken.None);
                            if (received.MessageType == 0x8)                             // close
                            {
                                await socket.CloseAsync(CancellationToken.None);
                                return;
                            }
                            else if (received.MessageType == 0x2)                             // binary
                            {
                                await socket.CloseAsync((int)WebSocketCloseStatus.InvalidMessageType, "Cannot accept binary frame", CancellationToken.None);
                            }
                            else
                            {
                                int count = received.Count;

                                while (received.EndOfMessage == false)
                                {
                                    if (count >= maxMessageSize)
                                    {
                                        throw new NotSupportedException();
                                    }

                                    received = await socket.ReceiveAsync(new ArraySegment <byte>(receiveBuffer, count, maxMessageSize - count), CancellationToken.None);
                                    count += received.Count;
                                }

                                var receivedString = Encoding.UTF8.GetString(receiveBuffer, 0, count);
                                var echoString = receivedString;
                                ArraySegment <byte> outputBuffer = new ArraySegment <byte>(Encoding.UTF8.GetBytes(echoString));
                                await socket.SendAsync(outputBuffer, (int)WebSocketMessageType.Text, true, CancellationToken.None);
                            }
                        }
                    });
                    return TaskHelpers.Completed();
                }
                else if (request.Path == "/Echo")
                {
                    var response = new OwinResponse(env);
                    return response.RespondString(HtmlContentEcho, "text/html");
                }
                else if (request.Path == "/")
                {
                    var response = new OwinResponse(env);
                    return response.RespondString(HtmlContent, "text/html");
                }
                else
                {
                    return next(env);
                }
            });
        }