优化消息推送事件

Signed-off-by: vilson <545522390@qq.com>
This commit is contained in:
vilson 2019-01-25 11:25:48 +08:00
parent 5e46cd67c7
commit 12f1c92f33

View File

@ -17,6 +17,7 @@
* 如果发现业务卡死可以将下面declare打开去掉//注释并执行php start.php reload * 如果发现业务卡死可以将下面declare打开去掉//注释并执行php start.php reload
* 然后观察一段时间workerman.log看是否有process_timeout异常 * 然后观察一段时间workerman.log看是否有process_timeout异常
*/ */
//declare(ticks=1); //declare(ticks=1);
use GatewayWorker\Lib\Gateway; use GatewayWorker\Lib\Gateway;
@ -36,11 +37,10 @@ class Events
*/ */
public static function onConnect($client_id) public static function onConnect($client_id)
{ {
// 向当前client_id发送数据
$data = ['action' => 'connect', 'data' => ['client_id' => $client_id]];
Gateway::sendToClient($client_id, json_encode($data));
// 向所有人发送 // 向所有人发送
// Gateway::sendToAll("$client_id login\r\n"); //getAllUidCount
$data = ['action' => 'connect', 'data' => ['client_id' => $client_id, 'online' => Gateway::getAllClientCount()]];
Gateway::sendToAll(json_encode($data));
} }
/** /**
@ -49,20 +49,22 @@ class Events
* @param mixed $message 具体消息 * @param mixed $message 具体消息
* @throws Exception * @throws Exception
*/ */
public static function onMessage($client_id, $message) public static function onMessage($client_id, $message)
{ {
// 向所有人发送 // 向所有人发送
Gateway::sendToAll("$client_id said $message\r\n"); $data = ['action' => 'onMessage', 'data' => ['client_id' => $client_id, 'msg' => "$client_id said $message\r\n"]];
} Gateway::sendToAll(json_encode($data));
}
/** /**
* 当用户断开连接时触发 * 当用户断开连接时触发
* @param int $client_id 连接id * @param int $client_id 连接id
* @throws Exception * @throws Exception
*/ */
public static function onClose($client_id) public static function onClose($client_id)
{ {
// 向所有人发送 // 向所有人发送
GateWay::sendToAll("$client_id logout\r\n"); $data = ['action' => 'onClose', 'data' => ['client_id' => $client_id, 'online' => Gateway::getAllClientCountindex.html()]];
} GateWay::sendToAll(json_encode($data));
}
} }