### 问题描述
### Swoole版本,PHP版本,以及操作系统版本信息
Swoole 4.45版本这样
php 最新的7.4 以上
系统 centos7 虚拟机 双核四线程 2G内存 磁盘是磁盘阵列RAID0的 2个盘 给他40GB空间
### 相关代码
websocket代码
```php
class web_socketer{
private $websocket;
public function __construct() {
$this->websocket = new swoole_websocket_server("0.0.0.0", 9501);
$this->websocket->set([
'worker_num' => 2, //开启2个worker进程
'max_request' => 4, //每个worker进程 max_request设置为4次
'task_worker_num' => 4, //开启4个task进程
'dispatch_mode' => 4, //数据包分发策略 - IP分配
'daemonize' => false, //守护进程(true/false)
]);
$this->websocket->on('Start', [$this, 'start']);
$this->websocket->on('Open', [$this, 'open']);
$this->websocket->on("Message", [$this, 'message']);
$this->websocket->on("Close", [$this, 'close']);
$this->websocket->on("Task", [$this, 'task']);
$this->websocket->on("Finish", [$this, 'finish']);
$this->websocket->start();
}
public function start($websocket) {
echo "#### 启动 ####".PHP_EOL;
echo "SWOOLE 版本".SWOOLE_VERSION . " 服务已启动".PHP_EOL;
echo "主进程 pid: {$websocket->master_pid}".PHP_EOL;
echo "管理者 pid: {$websocket->manager_pid}".PHP_EOL;
echo "########".PHP_EOL;
}
public function open($websocket, $request) {
echo "#### open ####".PHP_EOL;
echo "server: handshake success with fd{$request->fd}".PHP_EOL;
$websocket->task([
'type' => 'login'
]);
echo "########".PHP_EOL;
}
public function task($websocket, $task_id, $from_id, $data) {
echo "#### onTask ####".PHP_EOL;
echo "#{$websocket->worker_id} onTask: [PID={$websocket->worker_pid}]: task_id={$task_id}".PHP_EOL;
$msg = '';
switch ($data['type']) {
case 'login':
$msg = '我来了...';
break;
case 'speak':
$msg = $data['msg'];
break;
}
//已经连接的 客户
foreach ($websocket->connections as $fd) {
$connectionInfo = $websocket->connection_info($fd);
if ($connectionInfo['websocket_status'] == 3) {
$websocket->push($fd, $msg); //长度最大不得超过2M
}
}
$websocket->finish($data);
echo "########".PHP_EOL.PHP_EOL;
}
public function message($websocket, $frame) {
echo "#### onMessage ####".PHP_EOL;
echo "receive from fd{$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}".PHP_EOL;
$websocket->task(['type' => 'speak', 'msg' => $frame->data]);
echo "########".PHP_EOL.PHP_EOL;
}
public function finish($websocket,$task_id, $data) {
echo "#### onFinish ####".PHP_EOL;
echo "Task {$task_id} 已完成".PHP_EOL;
echo "########".PHP_EOL.PHP_EOL;
}
public function close($websocket, $fd) {
echo "#### close ####".PHP_EOL;
echo "client {$fd} closed".PHP_EOL;
echo "########".PHP_EOL.PHP_EOL;
}
}
$web_socketer = new web_socketer();
```
客户端代码
```php
发送消息
var index=0;
if ("WebSocket" in window) {
var wsServer = 'ws://192.168.0.110:9501';
var ws = new WebSocket(wsServer);
ws.onopen = function (evt) {
if (ws.readyState == 1) {
console.log('WebSocket 连接成功...');
} else {
console.log('WebSocket 连接失败...');
}
};
ws.onmessage = function (evt) {
console.log('接收到的数据: ' + evt.data+'->当前时间:'+get_time());
};
ws.onerror = function (evt) {
alert('WebSocket 发生错误');
console.log(evt);
};
ws.onclose = function() {
alert('WebSocket 连接关闭');
console.log('WebSocket 连接关闭...');
};
}else {
alert("您的浏览器不支持 WebSocket!");
}
function send_msg(){
index+=1;
for(i=0;i<10000;i++){
ws.send("你好啊 服务器 当前时间:"+get_time()+'->'+index.toString()+':'+i.toString());
}
}
function get_time(){
//定义时间变量对象mydate
var mydate=new Date();
var hour=mydate.getHours();
var minute=mydate.getMinutes();
var seconds=mydate.getSeconds();
return hour+":"+minute+":"+seconds;
}
```
图片很大
![](/storage/article-column/m5K7sBBeFji2ZvMaBihVqlSfQQWgJVItwih66r4Q.png)
### 你期待的结果是什么?实际看到的错误信息又是什么?
压测试一段时间后 延迟很大
刚开始 很快就能返回 对应信息
但是 压测一段时候后发现 一个消息返回需要 10秒以上了 swoole 也提示错误了 暂时不清楚咋解决 理论上 应该也不是我解决吧 应该是配置问题