百搜论坛欢迎您的加入!
adimg adimg
 
昨日:篇  今日:篇   总帖:篇   会员:
博主最大Lv63   
CRMEB商业版聊天模块的学习(一)     

首先看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
  • 1
  • 2
  • 3
  • 4

程序启动于think文件

#!/usr/bin/env php
<?php
namespace think;

// 加载基础文件
require __DIR__ . '/vendor/autoload.php';

// 应用初始化
(new App())->console->run();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

注意到$this->app->config->get从console.commands读配置,那配置文件在哪里呢?这就需要回过头来研读一下App.php这个文件了(文章所限,省略掉不重要的代码)

    public function initialize()
    {
        ......
        // 加载全局初始化文件
        $this->load();
        ......
	}

    protected function load(): void
    {
        ......
        foreach ($files as $file) {
            $this->config->load($file, pathinfo($file, PATHINFO_FILENAME));
        }
		.....
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

通过代码得知,程序加载时,即从/config目录下,把所有配置文件都加载进来,console.commands配置项即是console.php配置文件中的commands配置项。那我们就可以知道,聊天相关的对象是在\crmeb\command\Workerman这个类中。

下面来看一下Workerman类的execute方法,workerServer属性与chatWorkerServer长连接对象都是Worker对象实例,这个大家要学一下Workerman的资料了:

protected function execute(Input $input, Output $output)
    {
        $server = $this->init($input, $output);
        Worker::$pidFile = app()->getRootPath().'workerman.pid';
        if(!$server || $server == 'admin'){
            var_dump('admin');
            //创建 admin 长连接服务
            $this->workerServer = new Worker($this->config['admin']['protocol'] . '://' . $this->config['admin']['ip'] . ':' . $this->config['admin']['port']);
            $this->workerServer->count = $this->config['admin']['serverCount'];
        }

        if(!$server || $server == 'chat') {
            var_dump('chat');
            //创建 h5 chat 长连接服务
            $this->chatWorkerServer = new Worker($this->config['chat']['protocol'] . '://' . $this->config['chat']['ip'] . ':' . $this->config['chat']['port']);
            $this->chatWorkerServer->count = $this->config['chat']['serverCount'];
        }

        if(!$server || $server == 'channel') {
            var_dump('channel');
            //创建内部通讯服务
            $this->channelServer = new Server($this->config['channel']['ip'], $this->config['channel']['port']);
        }
        $this->bindHandle();
        try {
            Worker::runAll();
        } catch (\Exception $e) {
            $output->warning($e->getMessage());
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

注意:配置是从$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'];
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

/crmeb/service/workerman/chat/ChatService.php文件中,ChatService类用ChatHandle来进行业务相关操作,对于需要进行二次开发的小白来说,可以修改ChatHandle来完善业务功能,我感觉也可以通过传入的参数调用不同业务对象的功能,这样才能实现灵活扩展的目的。

 0  已被阅读了1133次  楼主 2020-07-30 13:41:13
回复列表

回复:CRMEB商业版聊天模块的学习(一)

联系站长 友链申请桂ICP备19000949号-1     桂ICP备19000949号-1
您的IP:18.219.132.200,2024-04-26 18:12:23,Processed in 0.01547 second(s).
免责声明: 本网不承担任何由内容提供商提供的信息所引起的争议和法律责任。
Powered by HadSky 7.12.9
已有0次打赏
(0) 分享
分享
取消
免责声明
1、本站资源,均来自网络,版权归原作者,所有资源和文章仅限用于学习和研究目的 。
2、不得用于商业或非法用途,否则,一切责任由该用户承担 !
如果觉得本文还不错请点个赞或者打赏点轻币哦~
拒绝伸手党,拿走请回复,尊重楼主,尊重你我他~

侵权删除请致信 E-Mail:207882320@qq.com