'websocket' => [
'enable' => true,
'handler' => Handler::class,
'parser' => Parser::class,
'ping_interval' => 25000,
'ping_timeout' => 60000,
'room' => [
'type' => 'redis',
'table' => [
'room_rows' => 4096,
'room_size' => 2048,
'client_rows' => 8192,
'client_size' => 2048,
],
'redis' => [
'host'=>'redis',
'prefix'=>'ws'
],
],
'listen' => [
//事件监听 一个socket事件对应一个php文件 文件类中 实现handle方法
//这种方式 事件一多 要配置许多文件,但精准快速
'login'=>Login::class
],
'subscribe' => [
//事件订阅 一个文件中可以写多个socket事件 php类方法对应socket事件名 类方法前+on
//这种方式是观察者模式
'app\listener\User'
],
],3. 再上两种方式的代码<?php
namespace app\listener;
use think\Container;
use think\swoole\Websocket;
class Login{
/**
* Undocumented variable
*
* @var \think\swoole\Websocket
* @author 秋月 414111907@qq.com
*/
public $websocket = null;
public function __construct(Container $container){
$this->websocket = $container->make(Websocket::class);
}
public function handle($event){
//dump($this->websocket->getFds());
// dump($event);
// dump($this->websocket->getSender());
//$this->websocket->emit("ddd",["aaa"=>1]);
}
}第二种<?php
namespace app\listener;
use think\Container;
use think\swoole\Websocket;
class User{
/**
* Undocumented variable
*
* @var Websocket
* @author 秋月 414111907@qq.com
*/
public $websocket = null;
public function __construct(Container $container){
$this->websocket = $container->make(Websocket::class);
}
public function onLogin(){
$this->websocket->emit("login",['mid'=>123]);
}
public function onPay(){
$this->websocket->emit("pay",['no'=>1233453452421]);
}
}注: tp6 swoole websocket这块难用,主要是没有手册的原因。有手册说明一下,很快就能上手。另外再参照一下tp6手册中的事件,很快就能明白了ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。