Ejemplo n.º 1
0
 public static IHandler Create(WebsocketHttpRequest request)
 {
     return(new ComposableHandler
     {
         Handshake = sub => Handshake(request, sub),
     });
 }
Ejemplo n.º 2
0
        public static byte[] Handshake(WebsocketHttpRequest request, string subProtocol)
        {
            Console.WriteLine("Building Draft76 Response");

            var builder = new StringBuilder();

            builder.Append("HTTP/1.1 101 WebSocket Protocol Handshake\r\n");
            builder.Append("Upgrade: WebSocket\r\n");
            builder.Append("Connection: Upgrade\r\n");
            builder.AppendFormat("Sec-WebSocket-Origin: {0}\r\n", request["Origin"]);
            builder.AppendFormat("Sec-WebSocket-Location: {0}://{1}{2}\r\n", request.Scheme, request["Host"], request.Path);

            if (subProtocol != null)
            {
                builder.AppendFormat("Sec-WebSocket-Protocol: {0}\r\n", subProtocol);
            }

            builder.Append("\r\n");

            var key1      = request["Sec-WebSocket-Key1"];
            var key2      = request["Sec-WebSocket-Key2"];
            var challenge = new ArraySegment <byte>(request.Bytes, request.Bytes.Length - 8, 8);

            var answerBytes = CalculateAnswerBytes(key1, key2, challenge);

            var byteResponse       = Encoding.ASCII.GetBytes(builder.ToString());
            var byteResponseLength = byteResponse.Length;

            Array.Resize(ref byteResponse, byteResponseLength + answerBytes.Length);
            Array.Copy(answerBytes, 0, byteResponse, byteResponseLength, answerBytes.Length);

            return(byteResponse);
        }
Ejemplo n.º 3
0
 public static IHandler Create(WebsocketHttpRequest request, Action <string> onMessage)
 {
     return(new ComposableHandler
     {
         TextFrame = FrameText,
         Handshake = sub => Handshake(request, sub),
         ReceiveData = data => ReceiveData(onMessage, data)
     });
 }
Ejemplo n.º 4
0
        public static IHandler Create(WebsocketHttpRequest request, Action <string> onMessage, Action onClose, Action <byte[]> onBinary, Action <byte[]> onPing, Action <byte[]> onPong)
        {
            var readState = new ReadState();

            return(new ComposableHandler
            {
                Handshake = sub => BuildHandshake(request, sub),
                TextFrame = s => FrameData(Encoding.UTF8.GetBytes(s), FrameType.Text),
                BinaryFrame = s => FrameData(s, FrameType.Binary),
                PingFrame = s => FrameData(s, FrameType.Ping),
                PongFrame = s => FrameData(s, FrameType.Pong),
                CloseFrame = i => FrameData(i.ToBigEndianBytes <ushort>(), FrameType.Close),
                ReceiveData = d => ReceiveData(d, readState, (op, data) => ProcessFrame(op, data, onMessage, onClose, onBinary, onPing, onPong))
            });
        }
Ejemplo n.º 5
0
        public static byte[] BuildHandshake(WebsocketHttpRequest request, string subProtocol)
        {
            Console.WriteLine("Building Hybi-14 Response");

            var builder = new StringBuilder();

            builder.Append("HTTP/1.1 101 Switching Protocols\r\n");
            builder.Append("Upgrade: websocket\r\n");
            builder.Append("Connection: Upgrade\r\n");
            if (subProtocol != null)
            {
                builder.AppendFormat("Sec-WebSocket-Protocol: {0}\r\n", subProtocol);
            }

            var responseKey = CreateResponseKey(request["Sec-WebSocket-Key"]);

            builder.AppendFormat("Sec-WebSocket-Accept: {0}\r\n", responseKey);
            builder.Append("\r\n");

            return(Encoding.ASCII.GetBytes(builder.ToString()));
        }
Ejemplo n.º 6
0
 public static byte[] Handshake(WebsocketHttpRequest request, string subProtocol)
 {
     // Console.WriteLine("Building Flash Socket Policy Response");
     return(Encoding.UTF8.GetBytes(PolicyResponse));
 }