swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.48k stars 3.16k forks source link

Question: Coroutine\Http\Server Settings #5284

Closed xuanyanwow closed 7 months ago

xuanyanwow commented 8 months ago

已知:

想了解的问题:

协程风格下是否支持 keep-alive 等参数的配置,进行 维持长连接

NathanFreeman commented 8 months ago

协程服务端是看客户端的请求头中的Connection来设置长链接的,不用手动设置

xuanyanwow commented 8 months ago

https://wiki.swoole.com/#/server/setting?id=open_tcp_keepalive

最终要解决的问题是检测死连接,通过这几个参数控制调整自己业务所能容忍的检测周期

NathanFreeman commented 8 months ago

这几个参数协程化服务端没法设置

xuanyanwow commented 8 months ago

那目前协程服务端在检测死连接的问题有做相关的处理么

NathanFreeman commented 7 months ago

目前不支持

xuanyanwow commented 7 months ago

有在计划中?

另一个问题:协程风格websocket server::close无法传参code和reason

// 异步风格
Swoole\WebSocket\Server->disconnect(int $fd, int $code = SWOOLE_WEBSOCKET_CLOSE_NORMAL, string $reason = ''): bool

【根据 RFC6455,对于应用程序关闭连接状态码,取值范围为 1000 或 4000-4999 之间】

matyhtf commented 7 months ago
  1. 协程 Server 建议使用 PHP 代码创建一个定时器,轮询切断超时的连接
  2. Response::push() 可以支持发送 SWOOLE_WEBSOCKET_OPCODE_CLOSE 类型的 frame,传递 code 和 reason
$server = new Co\Http\Server("127.0.0.1", 9502, false);
$server->handle('/websocket', function ($request, $ws) {
    $ws->upgrade();
    sleep(1);
    $frame = new Swoole\WebSocket\CloseFrame;
    $frame->reason = 'close';
    $ws->push($frame);
});