html5 - 本质就是websocket的客户端,php Websocket 怎么接收数据
程序员文章站
2022-04-04 20:41:49
...
class WebsocketClient
{
private $_Socket = null;
public function __construct($host, $port)
{
$this->_connect($host, $port);
}
public function __destruct()
{
$this->_disconnect();
}
public function sendData($data)
{
// send actual data:
return fwrite($this->_Socket, $this->encode($data)) or die('Error:' . $errno . ':' . $errstr);
$wsData = fread($this->_Socket, 2000);
$retData = trim($wsData, chr(0) . chr(255));
return $retData;
}
private function encode($data)
{
$data = is_array($data) || is_object($data) ? json_encode($data) : (string)$data;
$len = strlen($data);
$mask = array();
for ($j = 0; $j 127)
{
return false;
}
}
$head[1] += 128;
$head = array_merge($head, $mask);
foreach ($head as $k => $v)
{
$head[$k] = chr($v);
}
$mask_data = '';
for ($j = 0; $j _generateRandomString(32);
$key2 = $this->_generateRandomString(32);
$key3 = $this->_generateRandomString(8, false, true);
$header = "GET ws://" . $host . ":" . $port . "/ HTTP/1.1\r\n";
$header .= "Host: " . $host . ":" . $port . "\r\n";
$header .= "Connection: Upgrade\r\n";
$header .= "Pragma: no-cache\r\n";
$header .= "Cache-Control: no-cache\r\n";
$header .= "Upgrade: websocket\r\n";
$header .= "Sec-WebSocket-Version: 13\r\n";
$header .= "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36\r\n";
$header .= "Accept-Encoding: gzip, deflate, sdch\r\n";
$header .= "Accept-Language: zh-CN,zh;q=0.8\r\n";
$header .= "Sec-WebSocket-Key: " . $key1 . "\r\n";
$header .= "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n";
$header .= "\r\n";
$this->_Socket = fsockopen($host, $port, $errno, $errstr, 2);
fwrite($this->_Socket, $header) or die('Error: ' . $errno . ':' . $errstr);
$response = fread($this->_Socket, 2000);
/**
* @todo: check response here. Currently not implemented cause "2 key handshake" is already deprecated.
* See: http://en.wikipedia.org/wiki/WebSocket#WebSocket_Protocol_Handshake
*/
return true;
}
private function _disconnect()
{
fclose($this->_Socket);
}
private function _generateRandomString($length = 10, $addSpaces = true, $addNumbers = true)
{
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"§$%&/()=[]{}';
$useChars = array();
// select some random chars:
for ($i = 0; $i
能向服务器发送数组,怎么接收数据?
回复内容:
class WebsocketClient
{
private $_Socket = null;
public function __construct($host, $port)
{
$this->_connect($host, $port);
}
public function __destruct()
{
$this->_disconnect();
}
public function sendData($data)
{
// send actual data:
return fwrite($this->_Socket, $this->encode($data)) or die('Error:' . $errno . ':' . $errstr);
$wsData = fread($this->_Socket, 2000);
$retData = trim($wsData, chr(0) . chr(255));
return $retData;
}
private function encode($data)
{
$data = is_array($data) || is_object($data) ? json_encode($data) : (string)$data;
$len = strlen($data);
$mask = array();
for ($j = 0; $j 127)
{
return false;
}
}
$head[1] += 128;
$head = array_merge($head, $mask);
foreach ($head as $k => $v)
{
$head[$k] = chr($v);
}
$mask_data = '';
for ($j = 0; $j _generateRandomString(32);
$key2 = $this->_generateRandomString(32);
$key3 = $this->_generateRandomString(8, false, true);
$header = "GET ws://" . $host . ":" . $port . "/ HTTP/1.1\r\n";
$header .= "Host: " . $host . ":" . $port . "\r\n";
$header .= "Connection: Upgrade\r\n";
$header .= "Pragma: no-cache\r\n";
$header .= "Cache-Control: no-cache\r\n";
$header .= "Upgrade: websocket\r\n";
$header .= "Sec-WebSocket-Version: 13\r\n";
$header .= "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36\r\n";
$header .= "Accept-Encoding: gzip, deflate, sdch\r\n";
$header .= "Accept-Language: zh-CN,zh;q=0.8\r\n";
$header .= "Sec-WebSocket-Key: " . $key1 . "\r\n";
$header .= "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n";
$header .= "\r\n";
$this->_Socket = fsockopen($host, $port, $errno, $errstr, 2);
fwrite($this->_Socket, $header) or die('Error: ' . $errno . ':' . $errstr);
$response = fread($this->_Socket, 2000);
/**
* @todo: check response here. Currently not implemented cause "2 key handshake" is already deprecated.
* See: http://en.wikipedia.org/wiki/WebSocket#WebSocket_Protocol_Handshake
*/
return true;
}
private function _disconnect()
{
fclose($this->_Socket);
}
private function _generateRandomString($length = 10, $addSpaces = true, $addNumbers = true)
{
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"§$%&/()=[]{}';
$useChars = array();
// select some random chars:
for ($i = 0; $i
能向服务器发送数组,怎么接收数据?
http://www.workerman.net/workerman-chat
可以看下这个,PHP写的websocket聊天室,客服功能其实可以用里面的单聊功能实现 .有人用它实现过客服,包括网页和客户端软件。
开发手册:http://workerman.net/gatewaydoc/
Demo:http://chat.workerman.net/
百度上关于php websocket的教程已经有足够多了,你可以去看下。
socket_read读取
已解决非常感谢各位解答
上一篇: Nginx + LuaJIT + Redis 编译配置教程
下一篇: 如何实现自动加载jquery