elephant.io 里面已经封装好啦socket0.x 1.x 2.x对应的类安装最新版本就可以 github地址 https://github.com/wisembly/elephant.io
使用composer安装,打开composer.json加入下面包进行安装
"require": {
"wisembly/elephant.io": "~3.0"
},
使用方法
use ElephantIO\Client;
use ElephantIO\Engine\SocketIO\Version2X;
public function test(){
$client = new Client(new Version2X('https://****:19967', [
'headers' => [
'X-My-Header: websocket rocks',
'Authorization: Bearer 12b3c4d5e6f7g8h9i',
],
//连接超时时间
'timeout' => 5000,
]));
//初始化并建立连接
$client->initialize();
//发送一个conn请求
$client->emit('conn', ['uid' => 81252826, 'token' => '9917d7eb298ca324ed633325cb040c18', 'roomnum' => 6, 'stream' => '6_1603425280']);
$msg = [
'msg' => [
[
'_method_' => 'SendMsg',
'action' => 0,
'ct' => '聊天消息内容',
'msgtype' => '2',
'tougood' => '',
'touid' => '',
'touname' => '',
'ugood' => '这里填用户id',
'uid' => '这里填用户id',
'uname' => '小美',
'avatar_thumb' => 'http://*******/default_thumb.jpg',
'level' => '1',
'vip_type' => '0',
'liangname' => '0',
'usertype' => '30',
'guard_type' => '0',
],
],
'retcode' => '000000',
'retmsg' => 'OK',
];
//发送一个broadcast请求
$client->emit('broadcast',$msg);
//断开连接
$client->close();
}
经过测试可用,但是连接效率不是很高,小场景可以考虑使用,否则还是比较建议使用nodejs做客户端进行相关业务操作…