0

応答が WebBrowser に送信されたときに Web Socket 接続が確立されず、エラーが発生する: 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 に答える