简体   繁体   English

Websocket握手不起作用

[英]Websocket handshake doesnt work

i have a cpp server using WinSock2 and im trying to connect to this server with my javascript client, and it doesnt work, the chrome console says "Error during WebSocket handshake: Incorrect 'Sec-WebSocket-Accept' header value". 我有一个使用WinSock2的cpp服务器,我试图用我的javascript客户端连接到该服务器,并且它无法正常工作,chrome控制台显示“ WebSocket握手时出错:错误的'Sec-WebSocket-Accept'标头值”。 i compared my sha1 and base64 functions with online sha1 and base64 so the problem isnt here. 我将我的sha1和base64函数与在线sha1和base64进行了比较,因此问题不在这里。

Chrome Response Header: Chrome Response标头:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-Websocket-Accept: NzdkYjg1Y2I4MDRlNTk0OGNmNzI1NzdjZDgwOTEwZWZiYWI1NzQ3Yw==

Chrome Request Header: Chrome请求标头:

GET ws://localhost:8820/ HTTP/1.1
Host: localhost:8820
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: file://
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: he-IL,he;q=0.8,en-US;q=0.6,en;q=0.4
Sec-WebSocket-Key: Y7a2ZKEz/VCM92Wya49iPA==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

Server Code: 服务器代码:

//key is already defined.
key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
key = sha1(key);
key = base64_encode(reinterpret_cast<const unsigned char*>(key.c_str()), key.length());

toClient = "HTTP/1.1 101 Switching Protocols\r\n";
toClient += "Upgrade: websocket\r\n";
toClient += "connection: Upgrade\r\n";
toClient += "Sec-Websocket-Accept: ";
toClient += key; 
toClient += "\r\n\r\n";

sendData(sc, toClient);

Client Code: 客户代码:

<!DOCTYPE HTML>

  <script type="text/javascript">
     function WebSocketTest()
     {
        if ("WebSocket" in window)
        {
           //alert("WebSocket is supported by your Browser!");

           // Let us open a web socket
           var ws = new WebSocket("ws://localhost:8820");
           console.log("test");
           ws.onopen = function()
           {
                alert("Connection.")
                // Web Socket is connected, send data using send()
                ws.send("20304user04user04user");
                //alert("Message is sent...");
           };

           ws.onmessage = function (evt) 
           { 
              var received_msg = evt.data;
              alert("Hey");
           };

           ws.onclose = function()
           { 
              // websocket is closed.
              alert("Connection is closed..."); 
           };
        }

        else
        {
           // The browser doesn't support WebSocket
           alert("WebSocket NOT supported by your Browser!");
        }
     }
  </script>

  <div id="sse">
     <a href="javascript:WebSocketTest()">Run WebSocket</a>
  </div>

I tried manually to build the response: 我尝试手动构建响应:

  1. Raw sha1 digest bytes of the concatenation are 77db85cb804e5948cf72577cd80910efbab5747c 串联的原始sha1摘要字节为77db85cb804e5948cf72577cd80910efbab5747c
  2. Bytes to base 64: d9uFy4BOWUjPcld82AkQ77q1dHw= 以64为基数的字节: d9uFy4BOWUjPcld82AkQ77q1dHw=

This is different than what you have. 这与您所拥有的不同。 You are transforming the hexadecimal string representation of those bytes to base 64 instead of raw bytes to base 64. 您正在将这些字节的十六进制字符串表示形式转换为基数64,而不是将原始字节转换为基数64。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM