admin管理员组

文章数量:1326345

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.

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.

Share Improve this question edited Jun 30, 2014 at 6:38 Mr.Web asked Jun 30, 2014 at 6:16 Mr.WebMr.Web 7,1768 gold badges54 silver badges89 bronze badges 4
  • I don't know that library but since it seems you just want to establish a simple WS connection and send something maybe it would be better to use the default functionality. Take a look here for a simple WebSocket JS guide: tutorialspoint./html5/html5_websocket.htm – zewa666 Commented Jul 7, 2014 at 9:29
  • Hi, tks, that gives me this error WebSocket connection to 'ws://xxx.xxx.xxx.xxx:8830/' failed: Connection closed before receiving a handshake response. I've tried also ReconnectingSocket but still... – Mr.Web Commented Jul 7, 2014 at 10:10
  • That may indicate that A: the Socket is not running, B: the port is already occupied, C: handshaking not done properly (had same problem with a PHP implementation using different version), D: accessing cross domain may websockets may be prohibited by your browser settings. There are much more scenarios but those are the ones I can recall right now. Anyway I think the problem resides more on the server implementation. Without having more information about your server and the concrete implementation it is very hard to help you properly – zewa666 Commented Jul 7, 2014 at 21:19
  • 1 Does your server support websockets? – prashanth Commented Jul 13, 2014 at 20:33
Add a ment  | 

4 Answers 4

Reset to default 4 +100

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 ining 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./qibevu/1/edit if you change echo websocket server to your own.

Personally, I remend 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.

本文标签: javascriptjQuery Web Socket not connecting and not sendingStack Overflow