think-swoole应用教程核心思想是swoole只是作为一个音讯转发器,业务逻辑还是通过接口来实现,发送音讯也是应用接口,客户端websocket只负责创立和监听承受音讯即可。环境centos8PHP7.4thinkphp6.0.10think-swoole4.0.6开发过程装置think-swoole扩大为了不便咱们装置think-view扩大配置swoole.php文件
server.host 服务器IPserver.port 服务器端口server.options.daemonize 是否过程websocket.enable 关上websocketwebsocket.handle 本人接管或者应用默认(默认的会给咱们发送socket音讯,不理睬即可)websocket.subscribe 创立事件订阅,我这里的文件名是WebSocketEvent(也能够应用监听,只不过须要多个文件)因为是多过程,咱们须要共享变量,能够用MySQL、redis等,咱们这里应用swoole的共享内容Table,因为同一个用户可能是多端登录,咱们创立俩个Table,一个是用户映射fd,一个是fd映射用户,Table的映射是一对一的,然而一个用户可能有多个fd,所以用户映射fd的Table的值应用逗号分隔的多个值,例如用户1->fd1,fd2配置tables俩个table,别离是m2fd、fd2m,thinkphp实现的Table如何应用请本人看代码
'tables' => [ 'm2fd' => [ 'size' => 102400, 'columns' => [ ['name' => 'fd', 'type' => \Swoole\Table::TYPE_STRING, 'size' => 50] ] ], 'fd2m' => [ 'size' => 102400, 'columns' => [ ['name' => 'member_id', 'type' => \Swoole\Table::TYPE_INT] ] ],],通过订阅实现websocket逻辑把咱们须要应用的类通过构造函数依赖注入,方便使用咱们须要WebSocket类实现通信逻辑,Table类实现用户fd映射如果咱们应用了type为11的绑定形式,则订阅open事件,发送给客户端message事件办法体留空或者不写即可,咱们应用接口来实现逻辑close事件移除用户和fd的映射关系咱们定义一个事件,用于接口触发,从而实现发送音讯逻辑,事件名称叫做ApiEvent,代码如下
<?phpdeclare (strict_types = 1);namespace app\subscribe;use app\Request;use Swoole\Server;use think\swoole\Table;use think\swoole\Websocket;class WebSocketEvent{ private $websocket = null; private $m2fd = null; private $fd2m = null; public function __construct(Websocket $websocket, Table $table) { $this->websocket = $websocket; $this->m2fd = $table->get('m2fd'); $this->fd2m = $table->get('fd2m'); } // 这里之所以注入一个申请,是因为如果咱们不必type=11这种形式绑定,则能够通过new WebSocket的时候把用户ID传递过去,而后间接实现绑定 public function onOpen(Request $request) { $currentFd = $this->websocket->getSender(); $data = [ 'type' => 11, 'fd' => $currentFd ]; $this->websocket->push(json_encode($data)); } public function onClose() { $currentFd = $this->websocket->getSender(); // 通过fd找到用户ID $memberId = $this->fd2m->get((string)$currentFd, 'member_id'); // 如果没有找到映射,就阐明没有绑定过,就什么不做,找到的话就解除绑定 if ($memberId) { $this->fd2m->del((string)$currentFd); // 依据用户ID找到映射的所有fd,而后把存在的以后fd移除掉 $fds = $this->m2fd->get((string)$memberId, 'fd'); if ($fds) { $fdArray = explode(',', $fds); $key = array_search($currentFd, $fdArray); unset($fdArray[$key]); if ($fdArray) { $resFds = implode(',', $fdArray); $this->m2fd->set((string)$memberId, $resFds); } else { $this->m2fd->del((string)$memberId); } } } } public function onApiEvent($data) { // $data是接口传递过去的参数,如果是11则实现绑定,是5就转发给from_id和to_id if ($data['type'] == 11) { // m2fd、fd2m俩个Table的映射 $this->fd2m->set((string)$data['fd'], ['member_id' => $data['member_id']]); // 先查找该用户ID是否曾经绑定过其它fd了 $fds = $this->m2fd->get((string)$data['member_id'], 'fd'); if (!$fds) { $this->m2fd->set((string)$data['member_id'], ['fd' => $data['fd']]); } else { // 看看fd是否在曾经映射的fd中,如果在就什么都不做,如果不在就追加到前面 $fdArray = explode(',', $fds); if (!in_array($data['fd'], $fdArray)) { $this->m2fd->set((string)$data['member_id'], ['fd' => $fds . ',' . $data['fd']]); } } } if ($data['type'] == 1) { // 依据from_id和to_id俩个用户ID找到对应的fd,而后发送音讯 $fromFds = $this->m2fd->get((string)$data['from_id'], 'fd'); $toFds = $this->m2fd->get((string)$data['to_id'], 'fd'); $fromFdArray = $toFdArray = []; if ($fromFds) { $fromFdArray = explode(',', $fromFds); } if ($toFds) { $toFdArray = explode(',', $toFds); } // 合并所有发送者fd和接受者fd,之所以发送给发送者,一方面是简化前端工作,前端只须要承受websocket音讯即可,另一方面,多端的话其它端能够能够即时看到聊天记录 $allFdArray = array_unique(array_merge($fromFdArray, $toFdArray)); // 发送音讯 $this->websocket->to($allFdArray)->push(json_encode($data)); } }}接口实现代码如下
...