Skip to content

Instantly share code, notes, and snippets.

@stefanocudini
Created March 25, 2012 00:54
Show Gist options
  • Save stefanocudini/2190474 to your computer and use it in GitHub Desktop.
Save stefanocudini/2190474 to your computer and use it in GitHub Desktop.
Simple Websocket Client
<?
/* Simple Websocket Client
copyleft Stefano Cudini 2011-01-19
[email protected]
inspired by:
http://caucho.com/resin-4.0/examples/websocket-php/
*/
$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"
$retdata = trim($wsdata,"\x00\xff"); //extracts data
////WebSocket handshake
fclose($sock);
echo $retdata //data result
?>
@shirofuji
Copy link

have you tried making a client for wamp protocol?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment