我有一个工作 websocket 服务器(python + Tornado),它接受端口 8973 上的连接。我可以通过简单的 JavaScript / jquery 指令进行连接,例如:
ws = new WebSocket("ws://192.168.41.170:8973/rt");
但是我需要我的 php 脚本来连接到这个 websocket 服务器并发送一条消息。我尝试了所有最可用的解决方案,例如
https://github.com/lemmingzshadow/php-websocket/
$host = '192.168.41.170'; //where is the websocket server
$port = 8973;
$local = "http://192.168.41.2/"; //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);
但这一切都行不通。有没有人工作的代码片段?我不需要 php-websocket 服务器!谢谢