服务器响应时握手没有发生?

时间:2013-09-25 07:21:47

标签: google-chrome websocket handshaking

Web套接字连接未建立何时将响应发送到WebBrowser,获取 错误:未定义 DISCONNECTED

浏览器为:Google Chrome版本29.0.1547.76 m

请求标题1

GET / HTTP/1.1\r\n
Upgrade: websocket1\r\n
Connection: Upgrade1\r\n
Host: 192.168.56.103:80801\r\n
Origin: null1\r\n
Pragma: no-cache1\r\n
Cache-Control: no-cache1\r\n
Sec-WebSocket-Key: 8iyqe4WJXCbFS86VbpIaLw==1\r\n
Sec-WebSocket-Version: 131\r\n
Sec-WebSocket-Extensions: x-webkit-deflate-frame1\r\n
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.361\r\n
1\r\n

响应标题1

HTTP/1.1 101 Switching Protocols1\r\n
Connection: Upgrade1\r\n
Sec-WebSocket-Accept: taEMdeMpm/Lr4N0FdFjviakyN2o=1\r\n
Upgrade: websocket1\r\n
Sec-WebSocket-Extensions: x-webkit-deflate-frame1\r\n
Sec-WebSocket-Version: 131\r\n
Host: 192.168.56.103:80801\r\n
1\r\n

我尝试了一些简单的回复但是,连接没有建立

请求标头2

GET / HTTP/1.1\r\n
Upgrade: websocket\r\n
Connection: Upgrade\r\n
Host: 192.168.56.103:8080\r\n
Origin: null\r\n
Pragma: no-cache\r\n
Cache-Control: no-cache\r\n
Sec-WebSocket-Key: +vTyWP1c0t5S5WaThYZEMw==\r\n
Sec-WebSocket-Version: 13\r\n
Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36\r\n

响应标题2

HTTP/1.1 101 Switching Protocols\r\n
Connection: Upgrade\r\n
Sec-WebSocket-Accept: sQ2GgzduKdSSD6HE/QtHeBEjfOc=\r\n
Upgrade: websocket\r\n
\r\n

脚本如下:

<script type="text/javascript">

            var chatTextArea = document.getElementById("chatTextArea");

            function displayMessage(message)

            {

                chatTextArea.value += message + "\n";

                chatTextArea.scrollTop = chatTextArea.scrollHeight;

            }



            function sendMessage()

            {

                var pseudo = document.getElementById("inputPseudo").value;

                var msg = document.getElementById("inputText").value;

                var strToSend = pseudo + ": " + msg;

                if (websocket != null)

                {

                    document.getElementById("inputText").value = "";

                    websocket.send(strToSend);

                    console.log("Message sent :", '"'+strToSend+'"');

                }

            }



            var wsUri = "ws://192.168.56.103:8080";


            var websocket = null;


            function initWebsocket()

            {

                try

                {

                    if (typeof MozWebSocket == 'function')

                    {

                        WebSocket = MozWebSocket;

                    }

                    if (websocket && websocket.readyState == 1)

                    {

                        displayMessage("close");

                        websocket.close();

                    }

                    websocket = new WebSocket(wsUri);

                    websocket.onopen = function(evt) {

                        displayMessage("CONNECTED");

                    };

                    websocket.onclose = function(evt) {

                        displayMessage("DISCONNECTED");

                    };

                    websocket.onmessage = function(evt) {

                        console.log("Message received :", evt.data);

                        displayMessage(evt.data);

                    };

                    websocket.onerror = function(evt) {

                        displayMessage('ERROR: ' + evt.data);

                    };

                }

                catch(exception)

                {

                    displayMessage('ERROR: ' + exception);

                }

            }



            function stopWebsocket()

            {

                if (websocket)

                {

                    websocket.close();

                }

            }



            function checkSocket()

            {

                if (websocket != null)

                {

                    var stateStr;

                    switch (websocket.readyState)

                    {

                        case 0:

                            stateStr = "CONNECTING";

                            break;

                        case 1:

                            stateStr = "OPEN";

                            break;

                        case 2:

                            stateStr = "CLOSING";

                            break;

                        case 3:

                            stateStr = "CLOSED";

                            break;

                        default:

                            stateStr = "UNKNOW";

                            break;

                    }

                    displayMessage("Websocket state = " + websocket.readyState + " (" + stateStr + ")");

                }

                else

                {

                    displayMessage("Websocket is null");

                }

            }

        </script>

任何建议或帮助?

1 个答案:

答案 0 :(得分:0)

Sec-WebSocket-Accept计算似乎被打破了。请检查您的服务器实施。例如。对于请求1,Sec-WebSocket-Accept值应为“dysDTz162dZKjAglOk8QZ + 58 + 7o =”。