简体   繁体   中英

WebSocket server-side script call

WebSocket tutorials say that two things are required:

*1. Server-side script should be started:

php -q path/to/server.php

*2. Client-side script should open socket connection:

var socket = new WebSocket('ws://example.com:12345/server.php');

But requesting 'ws://example.com/server.php' will run server script one more time. It will cause running multiple server instances. Or client requests will cause server-side error (socket_bind(...) => already bound).

Can somebody explain this communication model? Thank you.

Websockets are persistent connections (they're designed to be persistent, though you still can abuse close frames to re-establish connection all the time...). (Have in maind that websockets are stateful as opposed to stateless HTTP)

So, when you open a websocket connection via new WebSocket('ws://example.com:12345/server.php'); , your webserver or socket listening script will then accept the connection. (The webserver must be in the same language than the code you're using, for websockets as they're persistnt connections, not stateless HTTP-like in-out cycles..)

Then it'll read your handshake request and decide upon these informations to where the request has to be routed.

So, your router usually should invoke some callback to delegate the initialization according to the specified path. You usually don't route to real files with websockets. You more define certain paths to be routed to their handlers.

You then map there your client socket usually to an user. (eg to send data to a certain user)

And so for your subsequent incoming data you just pass it with client state information to a callback previously determined according to route.


Though, with your question, it's relatively hard to guess what your problem exactly is. If it doesn't answer your question, please be more precise in your question. How it works is a bit too much generalized.

From what I undertand about websocket : when you run the server script, it will listen and react to all requests from the example.com domain name, on the 12345 port and with the ws:// protocol.

So, when you call new WebSocket('ws://example.com:12345/server.php'); from your client, it will send a request to the running server.php. In any case, it will not cause the server to run a second time.

I don't know about the details but, when I tried to run the websocket server script, it intercepted every request from ws://example.com:12345 . I didn't even need to specify the /server.php in the client call.

But I guess that's important if you want to make things clean, or if you may have several different scripts ?

Anyway, it is just a guess after a websocket try in my locahost ; I am not an websocket expert at all :)

basically, a WebSocket defines a full-duplex single socket connection over which messages can be sent between client and server.

you should read this:

http://es.wikipedia.org/wiki/WebSocket

and this:

http://www.websocket.org/

Ok so if your server.php file is working and you can see it listening on the port using netstat then first of all just connect using ws://example.com:1234 . Next you said "client waits for a long time and gets nothing" most likely something is wrong with the handshake process if that is the case. I had this same issue the first time I started playing with sockets in php. When the javascript connects to the websocket it sends a specific key in the headers it looks something like this:

Sec-WebSocket-Key: 9a9dufa9dfa90fa

You have to pull that out of the header append a string (yes you have to use that exact string 258EA... ) to it then sha1() it then convert it to base64, something like this:

if(preg_match("/Sec-WebSocket-Key: (.*)\r\n/", $headers, $match))
    $key = $match[1];
$acceptKey = $key.'258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
$acceptKey = base64_encode(sha1($acceptKey, true));

Next you need to write that back to the socket with some other info so that the javascript makes a handshake with the server:

$upgrade = "HTTP/1.1 101 Switching Protocols\r\n".
           "Upgrade: websocket\r\n".
           "Connection: Upgrade\r\n".
           "Sec-WebSocket-Accept: $acceptKey".
           "\r\n\r\n";
socket_write($the_socket, $upgrade); //$the_socket comes from socket_accept()

After that the handshake should complete and in your javascript the .onopen() function should trigger and you can send data back and forth between the client and the server:

var connection = new WebSocket('ws://127.0.0.1:1234');
connection.onopen = function () {
    console.log('Connection Opened');
    connection.send('Ping'); // Send the message 'Ping' to the server
};

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