1.顺序执行导包
composer require workerman/gateway-worker
composer require workerman/workerman
composer require workerman/gatewayclient
2.新建指令->application->admin->command->Workerman.php
<?phpnamespace appadmincommand;use Events;use thinkconsoleCommand;use thinkconsoleinputArgument;use appworkermancontrollerWorkerEvents;use GatewayWorkerBusinessWorker;use GatewayWorkerGateway;use GatewayWorkerRegister;use thinkconsoleInput;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' : ''; $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 = new Gateway("websocket://0.0.0.0:8282"); $gateway->name = 'Gateway'; $gateway->count = 1; $gateway->lanIp = '127.0.0.1'; $gateway->startPort = 2300; $gateway->pingInterval = 30; $gateway->pingNotResponseLimit = 0; $gateway->pingData = '{"type":"ping"}'; $gateway->registerAddress = '127.0.0.1:1236'; } private function startRegister() { new Register('text://0.0.0.0:1236'); }}
3.声明指令->application->command.php
<?phpreturn [ 'appadmincommandCrud', 'appadmincommandMenu', 'appadmincommandInstall', 'appadmincommandMin', 'appadmincommandAddon', 'appadmincommandApi', 'appadmincommandWorkerman',//此处];
4.新建回调控制器
更目录->application->workerman->controller->WorkerEvents.php
<?phpnamespace appworkermancontroller;use GatewayWorkerLibGateway;class WorkerEvents{ /** * 当客户端连接时触发 * 如果业务不需此回调可以删除onConnect * * @param int $client_id 连接id */ public static function onConnect(int $client_id) { } /** * 当客户端发来消息时触发 * @param int $client_id 连接id * @param mixed $message 具体消息 */ public static function onMessage(int $client_id, $message) { } /** * 当用户断开连接时触发 * @param int $client_id 连接id */ public static function onClose(int $client_id) { }}
5.启动命令
php think workerman start //已debug调试模式启动,结束窗口后会退出服务不常住 php think workerman start d // 以daemon(守护进程)方式启动 php think workerman stop php think workerman restart php think workerman reload php think workerman status
6.注意事项
启动服务器请在服务器云面板以及宝塔面板放行相关端口,请勿与其他端口公用,如果错误请指出
希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, Fastadmin, ClassCMS, LeCMS, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。