On this page
用户绑定客户端api
需要创建的文件
php
创建自定义events文件:\app\lib\socket\Events.php 用于处理gatewayworker
config/gateway_worker.php
php
return [
// 扩展自身需要的配置
'protocol' => 'websocket', // 协议 支持 tcp udp unix http websocket text
'host' => '0.0.0.0', // 监听地址
'port' => 23480, // 监听端口
'socket' => '', // 完整监听地址
'context' => [], // socket 上下文选项
'register_deploy' => true, // 是否需要部署register
'businessWorker_deploy' => true, // 是否需要部署businessWorker
'gateway_deploy' => true, // 是否需要部署gateway
// Register配置
'registerAddress' => '127.0.0.1:1236',
// Gateway配置
'name' => 'thinkphp',
'count' => 1,
'lanIp' => '127.0.0.1',
'startPort' => 2000,
'daemonize' => false,
'pingInterval' => 30,
'pingNotResponseLimit' => 0,
'pingData' => '{"type":"ping"}',
// BusinsessWorker配置
'businessWorker' => [
'name' => 'BusinessWorker',
'count' => 1,
'eventHandler' => '\app\lib\socket\Events',
],
];
\app\lib\socket\Events.php
php
<?php
namespace app\lib\socket;
use GatewayWorker\Lib\Gateway;
use Workerman\Worker;
use think\facade\Cache;
/**
* Worker 命令行服务类
*/
class Events
{
/**
* onConnect 事件回调
* 当客户端连接上gateway进程时(TCP三次握手完毕时)触发
*/
public static function onConnect($client_id)
{
// $data = [ 'type'=>'client_id', 'data'=>$client_id ];
// Gateway::sendToCurrentClient(json_encode($data));
}
/**
* onWebSocketConnect 事件回调
* 当客户端连接上gateway完成websocket握手时触发
*/
public static function onWebSocketConnect($client_id, $data)
{
// Gateway::sendToCurrentClient(json_encode([ 'type'=>'client_id', 'data'=>$client_id ]));
// var_export($client_id);
}
/**
* onMessage 事件回调
* 当客户端发来数据(Gateway进程收到数据)后触发
*/
public static function onMessage($client_id, $data)
{
//var_export($data);
// 验证当前客户端是否已经绑定
if (Gateway::getUidByClientId($client_id)) return;
$data = json_decode($data,true);
// 非法参数
if (!is_array($data) || !array_key_exists('type',$data) || !array_key_exists('token',$data) || $data['type'] !== 'bind' || empty($data['token'])) return;
$user = Cache::get($data['token']);
if (!$user) return Gateway::sendToCurrentClient(json_encode(['type'=>'bind','msg'=>'非法token,禁止操作','status'=>false]));
// 获取用户id
$userId = array_key_exists('type',$user) ? $user['user_id'] : $user['id'];
// 验证第三方是否绑定手机
if ($userId < 1) return Gateway::sendToCurrentClient(json_encode(['type'=>'bind','msg'=>'请先绑定手机','status'=>false]));
$User = \app\common\model\User::find($userId);
// 验证用户是否绑定手机
if (!$User->phone) return Gateway::sendToCurrentClient(json_encode(['type'=>'bind','msg'=>'请先绑定手机','status'=>false]));
// 验证用户状态
if ($User->status == 0) return Gateway::sendToCurrentClient(json_encode(['type'=>'bind','msg'=>'当前用户被禁用','status'=>false]));
// 绑定
Gateway::bindUid($client_id,$userId);
return Gateway::sendToCurrentClient(json_encode(['type'=>'bind','msg'=>'绑定成功','status'=>true]));
}
/**
* onClose 事件回调 当用户断开连接时触发的方法
*/
public static function onClose($client_id)
{
//GateWay::sendToAll("client[$client_id] logout\n");
}
/**
* onWorkerStop 事件回调
* 当businessWorker进程退出时触发。每个进程生命周期内都只会触发一次。
*/
public static function onWorkerStop(Worker $businessWorker)
{
echo "WorkerStop\n";
}
}