這里寫問題描述
webman框架寫一個電商項目,項目里做聊天和新訂單的消息提示。假設(shè)聊天同時在線人數(shù)幾百萬人以及新訂單消息也很多,量都很大 我的思路是聊天一個端口,新訂單一個端口,有給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當(dāng)作推送通道,除了上下線事件處理
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);
//發(fā)送消息
在控制器里調(diào)用GatewayClient調(diào)用gatewayWorer接口發(fā)送消息
GatewayClient::sendToUid($uid, $message);
但是這樣實現(xiàn)不了聊天走一個端口,消息提示走一個端口?。壳蠼獍??
配置一個websocket協(xié)議和frame協(xié)議 frame協(xié)議用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);
});
然后走h(yuǎn)ttp controller.php
當(dāng)有用戶下單后
Channel\Client::publish('notice_channel', ['toUid'=>1,'eventType'=>'newRecord']);