## 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绑定
}
}
~~~
- WebSocket协议
- 构造函数(6种协议)
- count(进程设置)
- name(链接名称)
- $daemonize(守护进程设置)
- logFile(日志路径)
- stdoutFile(守护进程记录文件)
- connections(获取链接数组的)
- worker的回调属性
- worker类的方法
- Connection类的方法
- getRemotePort获取端口方法
- getRemoteIp获取IP地址
- close 安全关闭连接
- 定时器
- Channel分布式通信组件
- 心跳检测程序
- liunx优化配置
- thinkphp5.1使用worerman
- thinkphp5.1中用Channel实现广播通信
- thinkphp5.1中使用定时器
- thinkphp5.1使用TcpConnection类
- Gateway类使用
- BusinessWorker使用
- Register类的使用
- Events类使用(业务逻辑层)
- Lib\Gateway 接口(经常用)
- webman中间件stomp
- Gateway在thinkphp5.1里使用