How to call a WebSocket programmatically (using PHP)?
Asked Answered
N

5

7

I have a situation where I need to update one browser window based on input from the other. Right now I'm using WebSockets and it's working great.

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

I have the following code which doesn't seem to work (the 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
    } 
Niece answered 19/4, 2011 at 0:12 Comment(0)
N
4

Here's how it's done:

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);
Niece answered 19/4, 2011 at 13:47 Comment(0)
H
1

In order to send data to the socket, you need to use fsockopen to open the connection to the socket at specified port. If the connection is successfully, all you need to do is use fwrite

However, you are going to be sending the data to the WebSocket server. The server will treat you as a client, and since you are not providing HTTP headers it expects for successful authentication - your connection will be refused.

Since you didn't say who is supposed to receive the message you are trying to send (all users or a specific user or something entirely different), without knowing what your goal is - it's hard to explain any further what you should do.

Hackney answered 19/4, 2011 at 14:7 Comment(0)
M
-1

There is a lot more to WebSockets than just sending the raw data to a TCP socket.

Ok, to start, you're using a UDP socket, where WebSockets use TCP. WebSockets is an entire protocol for communication, similar to HTTP, so you need to follow that protocol, there is a handshake step that you need to perform first and headers you need to add to all communication. It's not difficult, but I'm not going to go into detail here.

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

I'm not being rude, or mean, but in the future, try a quick Google search. That library I linked was found after googling "PHP WebSockets".

Medici answered 19/4, 2011 at 1:45 Comment(1)
Thanks, I'm actually using phpwebsocket already as the server. Will just try to copy the code from there to get it work. Thought it was simpler than that since I'm just sending one "packet" of data and that's it.Niece
P
-1

The most important part is that the message needs to be sent on the existing socket, meaning you cant call socket_connect, fsockopen, or any other function within PHP that will attempt an unsolicited connection to the client. This isn't a websocket thing - that's a fundamental concept in network programing.

On phpwebsocket it would be somethin like:

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

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

However, if for any reason you want to connect to the websocket server using websockets from PHP, you'll want to check out https://github.com/nicokaiser/php-websocket. The server in the link wont be of any importance if your happy with your current solution, but the package also contains a PHP Websocket client class which is what you would need.

Pemberton answered 8/12, 2012 at 23:56 Comment(0)
M
-1

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

Madlynmadman answered 12/10, 2013 at 5:19 Comment(1)
Reason for downvote: stackoverflow.com/help/how-to-answer -- Specifically "Answer the question" and "Provide context for links"Sustain

© 2022 - 2024 — McMap. All rights reserved.