From a247286b252017c86c7ae5a4f19019c89d6ee60f Mon Sep 17 00:00:00 2001
From: zhoudw <zhoudw@infobird.com>
Date: Mon, 21 Feb 2022 14:40:52 +0800
Subject: [PATCH] 配置
---
ws_server.php | 45 ++++++++++++++++++++++++++++++++-------------
1 files changed, 32 insertions(+), 13 deletions(-)
diff --git a/ws_server.php b/ws_server.php
index e129bd0..7eaa5a6 100644
--- a/ws_server.php
+++ b/ws_server.php
@@ -21,22 +21,41 @@
$this->redis_config = $config['redis'];
$this->server = new Swoole\WebSocket\Server($this->server_config['host'], $this->server_config['port']); // swoole连接
+ // $this->server->set(array(
+ // 'worker_num' => 8, //worker process num,设置启动的worker进程数量,测试时使用1个即可
+ // 'backlog' => 128, //listen backlog
+ // 'max_conn' => 100000, // Server最大允许维持多少个tcp连接。超过此数量后,新进入的连接将被拒绝
+ // 'max_request' => 0, // worker进程在处理完n次请求后结束运行,manager会重新创建一个worker进程。设置为0表示不自动重启。若在Worker进程中需要保存连接信息的服务,需要设置为0
+ // // dispatch_mode 配置在BASE模式是无效的,因为BASE不存在投递任务
+ // // 进程数据包分配模式: 1平均分配,2按FD取摸固定分配,3抢占式分配,默认为取摸(dispatch=2),4按IP分配,5按UID分配(需要用户代码中调用$serv->bind_uid(将一个连接绑定1个uid)
+ // 'dispatch_mode'=> 2,
+ // 'daemonize' => true, // 若设置为true,执行php server.php将转入后台作为守护进程运行
+ // 'log_file' => 'swoole-test_ws.log', // daemonize为true时,输出内容才会写入到该日志文件
+ // //'log_file' => 'swoole-test_ws.log', // daemonize为true时,输出内容才会写入到该日志文件
+ // //'heartbeat_idle_time' => 2000, // 连接最大允许空闲的时间(秒),与heartbeat_check_interval配合使用.当dispatch_mode=1/3时,底层会屏蔽onConnect/onClose事件
+ // //'heartbeat_check_interval' => 2, // 表示每隔多久轮循一次,单位为秒
+ // ));
+
$this->server->set([
- 'task_worker_num' => 8,
+ 'task_worker_num' => 4,
'enable_coroutine' => true,
- 'task_enable_coroutine' => true
+ 'task_enable_coroutine' => true,
+ 'max_conn' => 100000,
]);
- if (!$this->redis) {
- $this->redis = new Redis();
- $this->redis->connect($this->redis_config['host'],$this->redis_config['port'] );
- $host = $this->server_config['allow_ip'];
- $keyList = $this->redis->keys("*{$host}*");
- foreach ($keyList as $key => $value) {
- $this->redis->del($value);
+
+ $this->server->on('WorkerStart', function (Swoole\WebSocket\Server $server) {
+ if (!$this->redis) {
+ $this->redis = new Redis();
+ $this->redis->connect($this->redis_config['host'],$this->redis_config['port'] );
+ $host = $this->server_config['allow_ip'];
+ $keyList = $this->redis->keys("*{$host}*");
+ foreach ($keyList as $key => $value) {
+ $this->redis->del($value);
+ }
+ $this->redis->del("{$host}*");
+ $this->redis->close();
}
- $this->redis->del("{$host}*");
- $this->redis->close();
- }
+ });
$this->server->on('start', function (Swoole\WebSocket\Server $server) {
echo "Websocket Server is started at ws://".$this->server_config['host'].":".$this->server_config['port']."\n";
});
@@ -105,7 +124,7 @@
$url = $msgContent['url'];
$data = $msgContent['data'];
$action = substr($url, strrpos($url, "/") + 1);
- $this->log('action','ws','ws',$action);
+ // $this->log('action','ws','ws',$action);
switch ($action)
{
case "reg":
--
Gitblit v1.8.0