示例#1
0
        private static byte[] CreateServerHandshake(ServerHandshake handshake)
        {
            var stringShake = "HTTP/1.1 101 WebSocket Protocol Handshake\r\n" +
                              "Upgrade: WebSocket\r\n" +
                              "Connection: Upgrade\r\n" +
                              "Sec-WebSocket-Origin: " + handshake.Origin + "\r\n" +
                              "Sec-WebSocket-Location: " + handshake.Location + "\r\n";

            if (handshake.SubProtocol != null)
            {
                stringShake += "Sec-WebSocket-Protocol: " + handshake.SubProtocol + "\r\n";
            }
            stringShake += "\r\n";

            // generate a byte array representation of the handshake including the answer to the challenge
            byte[] byteResponse = Encoding.ASCII.GetBytes(stringShake);
            int byteResponseLength = byteResponse.Length;
            Array.Resize(ref byteResponse, byteResponseLength + handshake.AnswerBytes.Length);
            Array.Copy(handshake.AnswerBytes, 0, byteResponse, byteResponseLength, handshake.AnswerBytes.Length);
            return byteResponse;
        }
示例#2
0
        public static byte[] GenerateResponseHandshake(ClientHandshake clientHandshake)
        {
            var responseHandshake = new ServerHandshake();
            responseHandshake.Location = "ws://" + clientHandshake.Host + clientHandshake.ResourcePath;
            responseHandshake.Origin = clientHandshake.Origin;
            responseHandshake.SubProtocol = clientHandshake.SubProtocol;

            var challenge = new byte[8];
            Array.Copy(clientHandshake.ChallengeBytes.Array, clientHandshake.ChallengeBytes.Offset, challenge, 0, 8);

            responseHandshake.AnswerBytes = CalculateAnswerBytes(clientHandshake.Key1, clientHandshake.Key2, clientHandshake.ChallengeBytes);

            return CreateServerHandshake(responseHandshake);
        }