gatewayworker能不能聊天和消息提示走不同端口啊?怎么实现?

zhuyujin001

问题描述

这里写问题描述
webman框架写一个电商项目,项目里做聊天和新订单的消息提示。假设聊天同时在线人数几百万人以及新订单消息也很多,量都很大 我的思路是聊天一个端口,新订单一个端口,有给websocket开多端口的必要吗?没必要的开多端口的话,有没其他思路啊?

多端口我是这样写的:
我用的是gatewayworker连接websocket

配置config/plugin/gateway/process.php

 //chat聊天
    'chatServiceGateway' => [
        'handler'     => Gateway::class,
        'listen'      => 'websocket://0.0.0.0:7000',
        'count'       => 1,
        'reloadable'  => false,
        'constructor' => ['config' => [
            'lanIp'           => '127.0.0.1',
            'startPort'       => 2300,
            'pingInterval'    => 20,
            'pingData'        => new PingService,
            'registerAddress' => '127.0.0.1:1236',
            'onWorkerStart'   =>function($worker){ },
            'onWorkerStop'    =>function(){},                 

        ]]
    ], 
    // notice 新订单和新退款单、后台的消息通知
   'noticeGateway' => [
        'handler'     => Gateway::class,
        'listen'      => 'websocket://0.0.0.0:8000',
        'count'       => 1,
        'reloadable'  => false,
        'constructor' => ['config' => [
            'lanIp'           => '127.0.0.1',
            'startPort'       => 2400,
            'pingInterval'    => 20,
            'pingData'        => new PingService,
            'registerAddress' => '127.0.0.1:1236',
            'onWorkerStart'   =>function($worker){},
            'onWorkerStop'    =>function(){},                      

        ]]
    ], 

     'businesser' => [ 
        'handler'     => BusinessWorker::class,
        'count'       => 1,
        'constructor' => ['config' => [
            'eventHandler'    =>  shop\services\sockets\Events::class,
            'name'            => 'BusinessWorker',
            'registerAddress' => '127.0.0.1:1236',
        ]]
    ],  

shop\services\sockets\Events里只做绑定,把gatewayWorker当作推送通道,除了上下线事件处理

class Events
{
   public static function onConnect($client_id)
    {
         Gateway::sendToClient($client_id, json_encode(array(
            'type'      => 'clientBind',
            'clientId'  =>  $client_id

        ))); 

    }
}

//绑定
 Gateway::bindUid($client_id, $uid);   

//发送消息
 在控制器里调用GatewayClient调用gatewayWorer接口发送消息
 GatewayClient::sendToUid($uid, $message);

但是这样实现不了聊天走一个端口,消息提示走一个端口啊?求解啊?

164 2 0
2个回答

walkor 打赏

多端口一般用于多协议通讯,你这里开多端口没有意义。
另外电商项目不用考虑同时几百万人在线的情况,你又不是京东,几百人同时在线就很不错了。
电商类几百万人同时现在这种需要有至少要上百号人的技术团队,包括架构、运维、开发、测试人员。不是单一架构、随便一两个开发人员能搞定的。

  • zhuyujin001 2024-10-06

    谢谢大佬 在国庆假期还帮忙解答。按照你说的我又想了另一个思路,你看可行不?下面代码

zhuyujin001
配置一个websocket协议和frame协议 frame协议用workerman/channel
//chat聊天
    'chatServiceGateway' => [
        'handler'     => Gateway::class,
        'listen'      => 'websocket://0.0.0.0:7000',
        'count'       => 1,
        'reloadable'  => false,
        'constructor' => ['config' => [
            'lanIp'           => '127.0.0.1',
            'startPort'       => 2300,
            'pingInterval'    => 20,
            'pingData'        => new PingService,
            'registerAddress' => '127.0.0.1:1236',
            'onWorkerStart'   =>function($worker){ },
            'onWorkerStop'    =>function(){},                 

        ]]
    ], 
新订单的提示
'noticeGateway' => [
        'handler'     => ChannelServer::class,
        'listen'      => 'frame://0.0.0.0:9000',
        'count'       => 1,
        'reloadable'  => false,
        'constructor' => ['config' => [
            'lanIp'           => '127.0.0.1',
            'startPort'       => 2500,
            'pingInterval'    => 20,
            //'pingData'        => '{"type":"ping"}',
            'pingData'        => new PingService,
            'registerAddress' => '127.0.0.1:1236',
            'onWorkerStart'   =>function($worker){ },
            'onWorkerStop'    =>function(){}, 
        ]]
    ], 

EVENTS.php里
public static function onWorkerStart($worker)
    {
        _d('onWorkerStart');
        ChannelService::connet();
        //订阅事件
         Client::on('notice_channel', function ($eventData) use ($worker) {
            GatewayClient::sendToUid($eventData['toUid'], $message);
        });
然后走http controller.php
当有用户下单后
Channel\Client::publish('notice_channel', ['toUid'=>1,'eventType'=>'newRecord']);
  • walkor 2024-10-06

    加一个frame协议端口也没意义。channel不需要gateway开frame协议

×
🔝