コード例 #1
0
        private void BeginSendServerHandshake(ServerHandshake handshake, Socket socket)
        {
            var stringShake = "HTTP/1.1 101 Web Socket 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" +
                              "Sec-WebSocket-Accept: " + Encoding.ASCII.GetString(handshake.AnswerBytes) + "\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);

            socket.BeginSend(byteResponse, 0, byteResponse.Length, 0, EndSendServerHandshake, socket);
        }
コード例 #2
0
        private ServerHandshake GenerateResponseHandshake()
        {
            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);

            byte[] bytes;
            CalculateAnswerBytes(out bytes, ClientHandshake.Key1, ClientHandshake.Key2, ClientHandshake.ChallengeBytes, ClientHandshake.Version);
            responseHandshake.AnswerBytes = bytes;

            return(responseHandshake);
        }