合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
## confing/gateway_worker.php里修改 ~~~ <?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ] // +---------------------------------------------------------------------- // | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- // +---------------------------------------------------------------------- // | Workerman设置 仅对 php think worker:gateway 指令有效 // +---------------------------------------------------------------------- return [ // 扩展自身需要的配置 'protocol' => 'websocket', // 协议 支持 tcp udp unix http websocket text 'host' => '0.0.0.0', // 监听地址 'port' => 8881, // 监听端口 '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\gateway\Chat', ], ]; ~~~ ## Cache文件里 ~~~ <?php namespace app\gateway; use GatewayWorker\Lib\Gateway; use think\Cache; class Chat { //接收消息时候触发的方法 public static function onMessage($client_id, $data){ $data=json_decode($data,true); if($data['type']=='chatMessage'){ // $redis = Cache::store('redis')->handler(); $info['username'] = $data['data']['mine']['username']; $info['avatar'] = $data['data']['mine']['avatar']; $info['id'] = $data['data']['mine']['id']; $info['type'] = $data['data']['to']['type']; $info['content'] = $data['data']['mine']['content']; $info['mine'] = false; $info['fromid'] = $data['data']['mine']['id']; $info['timestamp'] = time()*1000; Gateway::sendToUid($data['data']['to']['id'],json_encode($info)); //向对方ID发消息 ,数据根据layim格式返回 } } //当可以连接时候传过来的数据 public static function onWebSocketConnect( $client_id, $data){ $token=$data['get']['token']; //自己的账号ID $key=config('chat.key'); $id=decrypt($token,$key); //解密好的ID Gateway::bindUid($client_id,$id);//机器ID与用户ID绑定 } } ~~~