项目有用到及时聊天 写一个聊天室
用GatewayWorker
1.引入依赖
composer require topthink/think-worker 1.0.1
(fastadmin用的是tp5.0 只能用1.0.1版本,希望早出tp6版本)
composer require workerman/gateway-worker
windows的话 还要
如果需要在window下做服务端,还需要
composer require workerman/workerman-for-win
composer require workerman/gateway-worker-for-win
2.自定义命令行
在/application/command 添加文件 Workerman.php
<?phpnamespace appcommand;use appworkermancontrollerWorkerEvents;use GatewayWorkerBusinessWorker;use GatewayWorkerGateway;use GatewayWorkerRegister;use thinkconsoleCommand;use thinkconsoleInput;use thinkconsoleinputArgument;use thinkconsoleinputOption;use thinkconsoleOutput;use thinkEnv;use WorkermanWorker;class Workerman extends Command{ protected function configure() { $this->setName('workerman') ->addArgument('action', Argument::OPTIONAL, "action start|stop|restart") ->addArgument('type', Argument::OPTIONAL, "d -d") ->setDescription('workerman chat'); } protected function execute(Input $input, Output $output) { global $argv; $action = trim($input->getArgument('action')); $type = trim($input->getArgument('type')) ? '-d' : ''; $argv[0] = 'chat'; $argv[1] = $action; $argv[2] = $type ? '-d' : '';// dump($argv);exit; $this->start(); } private function start() { $this->startGateWay(); $this->startBusinessWorker(); $this->startRegister(); Worker::runAll(); } private function startBusinessWorker() { $worker = new BusinessWorker(); $worker->name = 'BusinessWorker'; $worker->count = 1; $worker->registerAddress = '127.0.0.1:1236'; $worker->eventHandler = WorkerEvents::class; } private function startGateWay() { // gateway 进程 $gateway = new Gateway("websocket://0.0.0.0:39001"); // 设置名称,方便status时查看 $gateway->name = 'Gateway'; // 设置进程数,gateway进程数建议与cpu核数相同 $gateway->count = 4; // 分布式部署时请设置成内网ip(非127.0.0.1) $gateway->lanIp = '127.0.0.1'; // 内部通讯起始端口,假如$gateway->count=4,起始端口为4000 // 则一般会使用4000 4001 4002 4003 4个端口作为内部通讯端口 $gateway->startPort = 2300; // 心跳间隔 $gateway->pingInterval = 60; // 时间内,客户端未发送任何数据,断开客户端连接 $gateway->pingNotResponseLimit = 180; // 心跳数据 $gateway->pingData = '{"type":"ping"}'; // 服务注册地址 $gateway->registerAddress = '127.0.0.1:1238'; /* // 当客户端连接上来时,设置连接的onWebSocketConnect,即在websocket握手时的回调 $gateway->onConnect = function($connection) { $connection->onWebSocketConnect = function($connection , $http_header) { // 可以在这里判断连接来源是否合法,不合法就关掉连接 // $_SERVER['HTTP_ORIGIN']标识来自哪个站点的页面发起的websocket链接 if($_SERVER['HTTP_ORIGIN'] != 'http://kedou.workerman.net') { $connection->close(); } // onWebSocketConnect 里面$_GET $_SERVER是可用的 // var_dump($_GET, $_SERVER); }; }; */ } private function startRegister() { new Register('text://0.0.0.0:1236'); }}
3.编写事件
于admin 同级添加workerman/controller文件夹 新建文件WorkerEvents.php
<?phpnamespace appworkermancontroller;use GatewayWorkerLibGateway;class WorkerEvents{ /** * 当客户端连接时触发 * 如果业务不需此回调可以删除onConnect * * @param int $client_id 连接id */ public static function onConnect($client_id) { $data['type'] = 'get_client_id'; $data['data'] = ['client_id' => $client_id]; // 向当前client_id发送数据 Gateway::sendToClient($client_id, json_encode($data, JSON_UNESCAPED_UNICODE)); } /** * 当客户端发来消息时触发 * @param int $client_id 连接id * @param mixed $message 具体消息 */ public static function onMessage($client_id, $message) { dump("收到消息"); dump($client_id); dump($message); //不作处理 } /** * 当用户断开连接时触发 * @param int $client_id 连接id */ public static function onClose($client_id) { dump("断开连接".$client_id); }}
linux 的就算完成了
启动命令php think workerman start
守护命令php think workerman start d
windows的 还需要处理:
在项目根目录创建这四个文件
start_businessworker.php
<?phpdefine('APP_PATH', __DIR__ . '/application/');define('BIND_MODULE','workerman/Sbusinessworker');// 加载框架引导文件require __DIR__ . '/thinkphp/start.php';
start_gateway.php
<?phpdefine('APP_PATH', __DIR__ . '/application/');define('BIND_MODULE','workerman/Sgateway');// 加载框架引导文件require __DIR__ . '/thinkphp/start.php';
start_register.php
<?phpdefine('APP_PATH', __DIR__ . '/application/');define('BIND_MODULE','workerman/Sregister');// 加载框架引导文件require __DIR__ . '/thinkphp/start.php';
start_for_win.bat
<?phpphp start_register.php start_gateway.php start_businessworker.phppause
然后在workerman/controller 文件夹创建这几个文件
Sbusinessworker.php
<?phpnamespace appworkermancontroller;use WorkermanWorker;use GatewayWorkerBusinessWorker;use WorkermanAutoloader;class Sbusinessworker{ public function __construct(){ // bussinessWorker 进程 $worker = new BusinessWorker(); // worker名称 $worker->name = 'BusinessWorker'; // bussinessWorker进程数量 $worker->count = 4; // 服务注册地址 $worker->registerAddress = '127.0.0.1:1238'; //设置处理业务的类,此处制定Events的命名空间 $worker->eventHandler = 'appworkermancontrollerWorkerEvents'; // 如果不是在根目录启动,则运行runAll方法 if(!defined('GLOBAL_START')) { Worker::runAll(); } }}
Sgateway.php
<?phpnamespace appworkermancontroller;use WorkermanWorker;use GatewayWorkerGateway;use WorkermanAutoloader;class Sgateway{ public function __construct(){ // gateway 进程 $gateway = new Gateway("websocket://0.0.0.0:39001"); // 设置名称,方便status时查看 $gateway->name = 'Gateway'; // 设置进程数,gateway进程数建议与cpu核数相同 $gateway->count = 4; // 分布式部署时请设置成内网ip(非127.0.0.1) $gateway->lanIp = '127.0.0.1'; // 内部通讯起始端口,假如$gateway->count=4,起始端口为4000 // 则一般会使用4000 4001 4002 4003 4个端口作为内部通讯端口 $gateway->startPort = 2300; // 心跳间隔 $gateway->pingInterval = 100; // 时间内,客户端未发送任何数据,断开客户端连接 $gateway->pingNotResponseLimit = 100; // 心跳数据 $gateway->pingData = '{"type":"ping"}'; // 服务注册地址 $gateway->registerAddress = '127.0.0.1:1238'; /* // 当客户端连接上来时,设置连接的onWebSocketConnect,即在websocket握手时的回调 $gateway->onConnect = function($connection) { $connection->onWebSocketConnect = function($connection , $http_header) { // 可以在这里判断连接来源是否合法,不合法就关掉连接 // $_SERVER['HTTP_ORIGIN']标识来自哪个站点的页面发起的websocket链接 if($_SERVER['HTTP_ORIGIN'] != 'http://chat.workerman.net') { $connection->close(); } // onWebSocketConnect 里面$_GET $_SERVER是可用的 // var_dump($_GET, $_SERVER); }; }; */ // 如果不是在根目录启动,则运行runAll方法 if(!defined('GLOBAL_START')) { Worker::runAll(); } }}
Sregister.php
<?phpnamespace appworkermancontroller;use WorkermanWorker;use GatewayWorkerRegister;class Sregister{ public function __construct() { // register 服务必须是text协议 $register = new Register('text://0.0.0.0:1238'); // 如果不是在根目录启动,则运行runAll方法 if (!defined('GLOBAL_START')) { Worker::runAll(); } }}
配置完成 文件夹双击start_for_win.bat
只需要关注WorkerEvents.php 的方法就行 linux 和 windows 都是这一个文件
希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, Fastadmin, ClassCMS, LeCMS, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。