【问题标题】:jQuery Web Socket not connecting and not sendingjQuery Web Socket 未连接且未发送
【发布时间】:2014-06-30 06:16:36
【问题描述】:

我正在尝试将 PHP 函数转换为 jQuery 函数,以便可以直接从浏览器使用 TCP/IP 套接字本地连接。

我用这个:

$socket = @fsockopen('xxx.xxx.xxx.xxx', '8810', $err_no, $err_str);

if(!$socket){
    return 'Errore #'.$err_no.': '.$err_str;
}else{
    fwrite($socket, '["D";"C";"?ST";200]');
    $read = fread($socket, 2024);

     //other stuff...

    return $read;
    fclose($socket);

}

这很好用。然后我从 Google Code 网站下载了Github jQuery Websocket 0.0.4 并按照示例进行操作,但没有成功。

我只是尝试以这种方式建立连接并发送数据:

ws = $.websocket("ws://95.110.224.199:8810/");
ws.send('string', '["D";"C";"?ST";200]');

这给了我“未定义不是函数”错误。

然后我尝试查看连接是否真的以这种方式建立(不发送数据):

var ws = $.websocket("ws://xxx.xxx.xxx.xxx:8810/", {
    open: function() {console.log('WS:connesso')},
    close: function() {console.log('WS:chiuso')},
});

我运气不好...控制台什么也没说... 对此有任何提示或帮助吗?任何帮助表示赞赏。

【问题讨论】:

  • 我不知道那个库,但看起来你只是想建立一个简单的 WS 连接并发送一些东西,也许使用默认功能会更好。在这里查看一个简单的 WebSocket JS 指南:tutorialspoint.com/html5/html5_websocket.htm
  • 嗨,tks,这给了我这个错误WebSocket connection to 'ws://xxx.xxx.xxx.xxx:8830/' failed: Connection closed before receiving a handshake response。我也尝试过 ReconnectingSocket 但仍然...
  • 这可能表示 A:Socket 没有运行,B:端口已被占用,C:握手没有正确完成(使用不同版本的 PHP 实现也有同样的问题),D:访问您的浏览器设置可能会禁止跨域 websockets。还有更多的场景,但那些是我现在能回忆起的。无论如何,我认为问题更多在于服务器实现。如果没有关于您的服务器和具体实现的更多信息,很难正确地帮助您
  • 你的服务器支持 websockets 吗?

标签: javascript jquery sockets websocket


【解决方案1】:

也许您错过了 jquery.websocket.js 文件,但您可以使用 javascript 网络套接字:

ws = new WebSocket('ws://95.110.224.199:8810/'); 

或者你可以把 0.0.0.0 代替你的 ip,因为有些服务器不允许直接 ip 访问:

ws = new WebSocket('ws://0.0.0.0:8810/') 
ws.onopen = function(msg) {
    // Logic for opened connection
    console.log('Connection successfully opened');
};
ws.onmessage = function(msg) {
    // Handle received data
};

ws.onclose = function(msg) {
    // Logic for closed connection
    console.log('Connection was closed.');
}
ws.error =function(err){
    console.log(err); // Write errors to console
}

【讨论】:

  • 我总是遇到同样的错误:WebSocket connection to 'ws://000.000.000:8810/' failed: Connection closed before receiving a handshake response while in php works。
  • 你可以试试'ws://0.0.0.0:8810'
【解决方案2】:

WebSockets 不允许您像 PHP 中的 fsockopen 那样连接到任意 TCP 服务器。服务器还必须支持 WebSocket 协议,特别是:

  1. 如果服务器选择接受传入的连接,它必须 回复一个有效的 HTTP 响应,指示以下内容。

    1.  A Status-Line with a 101 response code as per RFC 2616
        [RFC2616].  Such a response could look like "HTTP/1.1 101
        Switching Protocols".
    
    2.  An |Upgrade| header field with value "websocket" as per RFC
        2616 [RFC2616].
    
    3.  A |Connection| header field with value "Upgrade".
    
    4.  A |Sec-WebSocket-Accept| header field.  The value of this
        header field is constructed by concatenating /key/, defined
        above in step 4 in Section 4.2.2, with the string "258EAFA5-
        E914-47DA-95CA-C5AB0DC85B11", taking the SHA-1 hash of this
        concatenated value to obtain a 20-byte value and base64-
        encoding (see Section 4 of [RFC4648]) this 20-byte hash.
    
        The ABNF [RFC2616] of this header field is defined as
        follows:
    
        Sec-WebSocket-Accept     = base64-value-non-empty
        base64-value-non-empty = (1*base64-data [ base64-padding ]) |
                                 base64-padding
        base64-data      = 4base64-character
        base64-padding   = (2base64-character "==") |
                           (3base64-character "=")
        base64-character = ALPHA | DIGIT | "+" | "/"
    

(来自WebSocket protocol specification

我怀疑您尝试连接的服务器不是特殊的 WebSocket 服务器,因此不会以 HTTP 响应响应客户端的握手。

【讨论】:

    【解决方案3】:

    您确定以正确的方式接受连接吗?

    请看How to call a WebSocket programmatically (using PHP)?,在交换数据开始时有东西发送到客户端,也许这是正确的方向。

    如果您的服务器一切正常,那么如果您将 echo websocket 服务器更改为您自己的,则此脚本应该可以正常工作http://jsbin.com/qibevu/1/edit

    【讨论】:

      【解决方案4】:

      就我个人而言,我推荐纯 javascript new WebSocket('ws://');,它易于使用,只要有后端套接字侦听相同的地址和端口,就会连接。

      【讨论】:

        猜你喜欢
        • 2012-04-13
        • 2013-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-27
        • 2020-09-22
        • 2019-12-05
        • 1970-01-01
        相关资源
        最近更新 更多