How to call WebSocket programmatically (using PHP)?

I have a situation where I need to update one browser window based on the input of another. I am using WebSockets now and it works great.

Now I want to send data to WebSocket using PHP instead of the browser (so instead of ws: // use the PHP code). In other words, I want to simulate a call to WebSocket.send () using PHP instead of JavaScript.

I have the following code that doesn't seem to work (onmessage is not being called):

if ( function_exists('socket_create') AND $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP) AND $sock_data = socket_connect($sock, "127.0.0.1", 12345) ) { $msg = "hello world"; $sock_data = socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, 1); //Set $sock_data = socket_write($sock, $msg, strlen($msg)); //Send data socket_close($sock); //Close socket } 
+4
source share
5 answers

Here's how to do it:

http://permalink.gmane.org/gmane.comp.lang.javascript.nodejs/18088

 $host = 'localhost'; //where is the websocket server $port = 9000; $local = "http://localhost/"; //url where this script run $data = 'hello world!'; //data to be send $head = "GET / HTTP/1.1"."\r\n". "Upgrade: WebSocket"."\r\n". "Connection: Upgrade"."\r\n". "Origin: $local"."\r\n". "Host: $host"."\r\n". "Content-Length: ".strlen($data)."\r\n"."\r\n"; //WebSocket handshake $sock = fsockopen($host, $port, $errno, $errstr, 2); fwrite($sock, $head ) or die('error:'.$errno.':'.$errstr); $headers = fread($sock, 2000); fwrite($sock, "\x00$data\xff" ) or die('error:'.$errno.':'.$errstr); $wsdata = fread($sock, 2000); //receives the data included in the websocket package "\x00DATA\xff" fclose($sock); 
+2
source

To send data to a socket, you need to use fsockopen to open a socket connection on the specified port. If the connection is successful, all you have to do is use fwrite

However, you are going to send data to the WebSocket server. The server will consider you as a client, and since you do not provide HTTP headers, it expects successful authentication - your connection will be rejected.

Since you did not say who should receive the message that you are trying to send (all users or a specific user or something completely different) without knowing what your goal is, it is difficult to explain what you need to do.

+1
source

WebSockets is much more than just sending raw data to a TCP socket.

Well, for starters, you are using a UDP socket, where WebSockets uses TCP. WebSockets is a whole protocol for communication, similar to HTTP, so you need to follow this protocol, there is a handshake step that you need to complete first, and headers that you need to add to all messages. It is not difficult, but I will not go into details here.

You have two options: implement the WebSockets protocol in php or use a built-in library like this one: http://code.google.com/p/phpwebsocket/

I'm not rude or don't mean it, but try a quick Google search in the future. This library that I linked to was found after googling "PHP WebSockets".

-1
source

The most important part is that the message must be sent to an existing socket, which means that you cannot call socket_connect, fsockopen or any other function in PHP that will try to make an unsolicited connection to the client. This is not a thing of the web cluster - it is a fundamental concept of network programming.

In phpwebsocket, it will be something like this:

 $msg = "hello world"; $phpwebsocket->send($user->socket, $msg); 

where '$ phpwebsocket' is the PHP WebSocket object, $ user-> socket is the connected user who connected to priory using javascript WebSocket (), and send () is the method in the WebSocket object that will correctly encode the message into a frame ( or should be as soon as possible).

However, if for some reason you want to connect to the websocket server using websockets from PHP, you need to check https://github.com/nicokaiser/php-websocket . The server in the link will not make any difference if you are happy with your current solution, but the package also contains the PHP Websocket client class that you need.

-1
source

Checkout ratchet You can use something like telnet with popen / proc_open to communicate with the socket server.

-1
source

All Articles