JavaScript WebSocket会立即关闭连接

JavaScript WebSocket会立即关闭连接,javascript,html,node.js,websocket,http-1.1,Javascript,Html,Node.js,Websocket,Http 1.1,我正在编写自己的脚本,使用websocket API使用JavaScript连接到websocket服务器。我遇到了直接关闭连接的问题 以下是客户端脚本: var host = 'ws://localhost:8080'; try { debug.add('Connection request submitted for ' + host); socket = new WebSocket(host); debug.readyStateListener(); de

我正在编写自己的脚本,使用websocket API使用JavaScript连接到websocket服务器。我遇到了直接关闭连接的问题

以下是客户端脚本:

var host = 'ws://localhost:8080';
try
{
    debug.add('Connection request submitted for ' + host);
    socket = new WebSocket(host);
    debug.readyStateListener();

    debug.add('Socket request started');

    socket.onopen = function()
    {
        debug.add('Connection opened');
    }

    socket.onmessage = function(message)
    {
        debug.add('data received ' + message.data);
    }
    socket.onclose = function()
    {
        debug.add('Connection closed');
    }
}
catch(e)
{
    debug.add('WebSockets error ' + e.toString() );
}
这是我收到的调试:

Connection request submitted for ws://localhost:8080
socket readyState change to 0
Socket request started
socket readyState change to 3
Connection closed
debug.readyStateListener()轮询socket.readyState以查看更改。发生的情况是,它更改为0表示连接正在打开,然后立即更改为3表示连接已关闭

服务器可以很好地接收连接,但是客户端会立即关闭连接


我在Opera11中尝试过,启用了WebSockets,并在最新版本的Chrome中尝试过。两次我得到的结果都一样

我可以通过原始连接与服务器完美通信,或者只需在我的浏览器中访问即可。结果如下:

GET / HTTP/1.1
User-Agent: Opera/9.80 (Windows NT 6.1; U; IBM EVV/3.0/EAK01AG9/LE; en) Presto/2.9.168 Version/11.51
Host: localhost:8080
Accept: application/xhtml+voice+xml;version=1.2, application/x-xhtml+voice+xml;version=1.2, text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1
Accept-Language: nl-NL,nl;q=0.9,en;q=0.8
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
通过http发送请求,直到我结束连接,连接也保持活动状态,没有任何缺陷

通过JavaScript WebSocket API,服务器收到以下请求:

GET / HTTP/1.1
Host: localhost:8080
Origin: http://localhost
Upgrade: WebSocket
Sec-WebSocket-key1: L58(b Q]'9 4 9\  0 *+ 6 a4
Connection: Upgrade
Sec-WebSocket-Key2: \+ 1 5d/9541840N*4

我最后的猜测是连接:升级或升级:客户端不正确支持WebSocket。对我来说,接收连接更符合逻辑:保持活动状态,但我不知道如何重新喜欢它。

备选方案:你看过吗?“在最新版本的Chrome中”-Chrome使用了新的草稿,它没有使用
Sec-WebSocket-key1
-key2
,而是使用基本64编码版本。这个草案版本是一个彻底的修改,是最终版本。你最好实施这个计划。它更复杂,但您正在实现的版本已被弃用。无论如何,如果您仍然使用旧的草稿,那么您是否正确地计算了响应键?你能把握手应答键的密码贴出来吗?