简体   繁体   中英

jQuery Web Socket not connecting and not sending

I'm trying to convert a PHP function into a jQuery function so that I can use a TCP/IP socket local connection straight from the browser.

I use this:

$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);

}

This works fine. I then downloaded the Github jQuery Websocket 0.0.4 from the Google Code site and followed the example but not successfully.

I just simply tried to make a connection and send data this way:

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

This gives me "undefined is not a function" error.

I then tried to see if the connection was actually establishing (without sending data) this way:

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

No luck for me... the console says nothin'... Any hint or help on this? Any help is appreciated.

Maybe you missed the jquery.websocket.js file but you can use a javascript web socket:

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

Or you can put 0.0.0.0 in instead of your ip because some servers do not allow direct ip access:

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
}

WebSockets doesn't allow you to connect to an arbitrary TCP server in the way fsockopen from PHP does. The server must also support the WebSocket protocol, in particular:

  1. If the server chooses to accept the incoming connection, it MUST reply with a valid HTTP response indicating the following.

    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 | "+" | "/"

(from the WebSocket protocol specification )

I suspect that the server you're trying to connect isn't a special WebSocket server, and thus doesn't reply with a HTTP response to the client's handshake.

Are you sure you are accepting connection in right way?

Please take a look at How to call a WebSocket programmatically (using PHP)? , there is something sent to the client at the beginning of exchanging data, maybe that's right direction.

If everything with your server is ok, then this script should work fine http://jsbin.com/qibevu/1/edit if you change echo websocket server to your own.

Personally, I recommend the pure javascript new WebSocket('ws://'); it's easy to use and will connect as long as there's a backend socket listening on the same address and port.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM