undefinedfix
Sign in

How to add tasks at startup of spool

altegn0 edited in Thu, 24 Nov 2022
$server = new Swoole\WebSocket\Server("0.0.0.0", 9501);

$server->on('open', function (Swoole\WebSocket\Server $server, $request) {
    echo "server: handshake success with fd{$request->fd}\n";
});
//心跳等配置
$server->set([
    'heartbeat_check_interval' => 5,
    'heartbeat_idle_time' => 10,
]);

$server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
    echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
    $server->push($frame->fd, "this is server");
});

$server->on('close', function ($ser, $fd) {
    echo "client {$fd} closed\n";
});

//我想加一个类似心跳检测的功能,就是启动后就轮询执行的功能 我要怎么加呢?
$server->start();
2 Replies
hsden
commented on Thu, 24 Nov 2022

The parameter you add now is heartbeat_ idle_ Time '= > 10 if a connection does not send any data to the server within 10 seconds, the connection will be forced to close' heartbeat '_ check_ Interval '= > 5 traverses every 5 seconds

Dafe
commented on Fri, 25 Nov 2022

I perform the timing task in the OnStart callback as follows:

 public function onStart($server) {
    MainTimerTask::start();//启动定时任务
 }