You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.5 KiB
85 lines
2.5 KiB
2 months ago
|
<?php
|
||
|
/**
|
||
|
* This file is part of workerman.
|
||
|
*
|
||
|
* Licensed under The MIT License
|
||
|
* For full copyright and license information, please see the MIT-LICENSE.txt
|
||
|
* Redistributions of files must retain the above copyright notice.
|
||
|
*
|
||
|
* @author walkor<walkor@workerman.net>
|
||
|
* @copyright walkor<walkor@workerman.net>
|
||
|
* @link http://www.workerman.net/
|
||
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
* 用于检测业务代码死循环或者长时间阻塞等问题
|
||
|
* 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload
|
||
|
* 然后观察一段时间workerman.log看是否有process_timeout异常
|
||
|
*/
|
||
|
//declare(ticks=1);
|
||
|
|
||
|
use \GatewayWorker\Lib\Gateway;
|
||
|
|
||
|
/**
|
||
|
* 主逻辑
|
||
|
* 主要是处理 onConnect onMessage onClose 三个方法
|
||
|
* onConnect 和 onClose 如果不需要可以不用实现并删除
|
||
|
*/
|
||
|
class Events
|
||
|
{
|
||
|
/**
|
||
|
* 当客户端连接时触发
|
||
|
* 如果业务不需此回调可以删除onConnect
|
||
|
*
|
||
|
* @param int $client_id 连接id
|
||
|
*/
|
||
|
public static function onConnect($client_id)
|
||
|
{
|
||
|
echo "客户端连接成功了 === client_id = ".$client_id . PHP_EOL;
|
||
|
// 向当前client_id发送数据
|
||
|
Gateway::sendToClient($client_id, "你的连接ID为: $client_id ");
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 当客户端发来消息时触发
|
||
|
* @param int $client_id 连接id
|
||
|
* @param mixed $message 具体消息
|
||
|
*/
|
||
|
public static function onMessage($client_id, $message)
|
||
|
{
|
||
|
echo "客户端".$client_id."发送的消息 === ".$message . PHP_EOL;
|
||
|
|
||
|
$data = json_decode($message, true);
|
||
|
$type = isset($data['type']) ? $data['type'] : '';
|
||
|
$user_id = isset($data['uid']) ? $data['uid'] : 0;
|
||
|
|
||
|
// 将用户ID与连接绑定
|
||
|
if ($user_id > 0) {
|
||
|
Gateway::bindUid($client_id, $user_id);
|
||
|
}
|
||
|
|
||
|
// 根据消息类类型处理逻辑
|
||
|
switch ($type) {
|
||
|
case 'pop-up': // 弹窗通知
|
||
|
echo "弹窗通知事件";
|
||
|
$res = Gateway::sendToAll("广播弹窗消息");
|
||
|
var_dump($res);
|
||
|
break;
|
||
|
default:
|
||
|
echo "客服端发送的消息type无法处理...".PHP_EOL;
|
||
|
Gateway::sendToCurrentClient("客服端发送的消息type无法处理");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 当用户断开连接时触发
|
||
|
* @param int $client_id 连接id
|
||
|
*/
|
||
|
public static function onClose($client_id)
|
||
|
{
|
||
|
// 向所有人发送
|
||
|
// GateWay::sendToAll("$client_id logout\r\n");
|
||
|
}
|
||
|
}
|