转载地址:https://www.cnblogs.com/shenqilun/articles/11322858.html
参考地址:
1.https://blog.csdn.net/qq_38882327/article/details/89352916
2.http://doc2.workerman.net/326107
3.https://blog.csdn.net/sinat_34469308/article/details/84799728 (这个也很有用 绑定的方法不一样)
4.http://www.ptbird.cn/gateway-worker-many-people-chat-online-group.html
5.https://blog.csdn.net/qq_38211838/article/details/82020708
一、下载
1、GatewayWorker
百度网盘
链接:https://pan.baidu.com/s/1L0cK6ol2Cx-h34ZzLGQ_Sg
提取码:bgrb
官方
https://www.workerman.net/download/GatewayWorker.zip
2、thinkphp3.2.3
链接:https://pan.baidu.com/s/1K7XPQbOdUibVvGOtDYm9LQ
提取码:3xg0
二、GatewayWorker thinkphp3.2 整合
1、复制GatewayWorker\vendor\workerman\gateway-worker\src到ThinkPHP\Library文件夹下,更名为GatewayWorker。除了DbConnection.php之外的所有文件全部改成.class.php结尾。
2、复制GatewayWorker\vendor\workerman\workerman到ThinkPHP\Library文件夹下,更名为Workerman。除了Constants.php之外的所有文件全部改成.class.php结尾。
2.1、将Worker.class.php文件的831行-836行改为
if (!isset($argv[2]) || !in_array($argv[2],
$available_commands)) {
exit($usage);
}
2.2、将Worker.class.php文件的831行-836行改为839行-840行改为
$command = trim($argv[2]);
$command2 = isset($argv[3]) ? $argv[3] : '';
三、源码
Application\Demo\Controller\ManController.class.php
display();
}
/**
* client_id与uid绑定操作
*/
public function bind(){
$client_id = I('client_id');
$uid = session('uid');
//client_id与uid绑定操作
Gateway::bindUid($client_id, $uid);
$message = '绑定成功' . $uid . '-' . $client_id;
//推送成功消息
Gateway::sendToUid($uid, json_encode($message));
// 加入某个群组(可调用多次加入多个群组)
//Gateway::joinGroup($client_id, $group_id);
}
/**
* 发送信息
*/
public function sendmsg(){
$uid = session('uid');
$message = '222';
//向任意uid的网站页面发送数据
Gateway::sendToUid($uid, $message);
//向所有用户发送
//Gateway::sendToAll($message);
//向某个client_id对应的连接发消息
//Gateway::sendToClient($client_id, $data);
// 向任意群组的网站页面发送数据
//Gateway::sendToGroup($group, $message);
}
}
Application\Demo\SocketController.class.php
startRegister();
// $this->startBusinessWorker();
// $this->startGateWay();
// Worker::runAll();
}
public function startRegister(){
// new Register('text://lhygtest.com:1237');//本地
new Register('text://lhygtest.com:1237');//正式
Worker::runAll();
}
public function startBusinessWorker(){
$worker = new BusinessWorker();
$worker->name = 'BusinessWorker';// worker名称
$worker->count = 1;// bussinessWorker进程数量
// $worker->registerAddress = '127.0.0.1:1237';// 本地服务注册地址
$worker->registerAddress = '127.0.0.1:1237';// 正式服务注册地址
$worker->eventHandler = Events::class;//设置处理业务的类,此处制定Events的命名空间
Worker::runAll();
}
public function startGateWay(){
// $gateway = new Gateway("Websocket://0.0.0.0:7272");// 本地 gateway 进程
$gateway = new Gateway("websocket://lhygtest.com:8282");// 正式 gateway 进程
$gateway->name = 'Gateway';// 设置名称,方便status时查看
$gateway->count = 4;// 设置进程数,gateway进程数建议与cpu核数相同
// $gateway->lanIp = '127.0.0.1';// 本地分布式部署时请设置成内网ip(非192.168.10.149)
$gateway->lanIp = '127.0.0.1';// 正式分布式部署时请设置成内网ip(非192.168.10.149)
$gateway->startPort = 2900;// 内部通讯起始端口,假如$gateway->count=4,起始端口为4000
$gateway->pingInterval = 100;// 心跳间隔
$gateway->pingData = '{"type":"ping"}';// 心跳数据
// $gateway->registerAddress = '127.0.0.1:1237';// 本地服务注册地址
$gateway->registerAddress = '127.0.0.1:1237';// 正式服务注册地址
Worker::runAll();
}
}
Application\Demo\Controller\EventsController.class.php
'init',
'client_id' => $client_id
)));
}
// GatewayWorker建议不做任何业务逻辑,onMessage留空即可
public static function onMessage($client_id, $message)
{
}
}
Application/Demo/View/Man/index.html
WebSocket示例
四、运行
1、Liunx下进入网站根目录(与index.php入口文件同级目录),执行命令 php index.php Home/Socket/start start
- windows下 进入网站根目录(与index.php入口文件同级目录),执行命令
php index.php Home/Socket/startRegister start
php index.php Home/Socket/startBusinessWorker start
php index.php Home/Socket/startGateWay start