0

向 WebBrowser 发送响应时未建立 Web Socket 连接,出现 ERROR: undefined 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>

有什么建议或帮助吗?

4

1 回答 1

0

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

于 2013-10-21T16:11:59.910 回答