源码分享:http://github.crmeb.net/u/defu
首先看CRMEB驻留内存的bat
start cmd /c php think workerman start chat
start cmd /c php think workerman start admin
start cmd /c php think timer start
php think workerman start channel
程序启动于think文件
#!/usr/bin/env php
<?php namespace think; // 加载基础文件 require __DIR__ . '/vendor/autoload.php'; // 应用初始化 (new App())->console->run(); CRMEB基于ThinkPHP,ThinkPHP按composer的模式整理了程序的启动过程,有空的小白应该可能去认真研读一下autoload.php内部的相关代码,这样对学习进程会很有帮助 通过命名空间think找到vendor/topthink/framework/src/think/Console.php文件,Console类中,__construct函数通过loadCommands函数加载了配置 /** * 加载指令 * @access protected */ protected function loadCommands(): void { $commands = $this->app->config->get('console.commands', []); $commands = array_merge($this->defaultCommands, $commands); $this->addCommands($commands); } 注意:配置是从$this->config[‘admin’][‘protocol’],查找配置文件的目录workerman.php配置文件包含了这些配置项,配置项是通过config函数来读的,config函数是在/vendor/topthink/framework/src/helper.php文件中,也是用Config类读取配置的 从bindHandle方法中,得知ChatService是接收聊天对话的业务类 protected function bindHandle() { if(!is_null($this->workerServer)){ $server = new WorkermanService($this->workerServer, $this->channelServer); // 连接时回调 $this->workerServer->onConnect = [$server, 'onConnect']; // 收到客户端信息时回调 $this->workerServer->onMessage = [$server, 'onMessage']; // 进程启动后的回调 $this->workerServer->onWorkerStart = [$server, 'onWorkerStart']; // 断开时触发的回调 $this->workerServer->onClose = [$server, 'onClose']; } if(!is_null($this->chatWorkerServer)) { $chatServer = new ChatService($this->chatWorkerServer, $this->channelServer); $this->chatWorkerServer->onConnect = [$chatServer, 'onConnect']; $this->chatWorkerServer->onMessage = [$chatServer, 'onMessage']; $this->chatWorkerServer->onWorkerStart = [$chatServer, 'onWorkerStart']; $this->chatWorkerServer->onClose = [$chatServer, 'onClose']; } } /crmeb/service/workerman/chat/ChatService.php文件中,ChatService类用ChatHandle来进行业务相关操作,对于需要进行二次开发的小白来说,可以修改ChatHandle来完善业务功能,我感觉也可以通过传入的参数调用不同业务对象的功能,这样才能实现灵活扩展的目的。