hyperf / hyperf

🚀 A coroutine framework that focuses on hyperspeed and flexibility. Building microservice or middleware with ease.

Home Page:https://www.hyperf.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[QUESTION] websocket onclose 如何获取到关闭连接的code和reason

caijwei opened this issue · comments

按照websocket协议,关闭连接时会(可以)有一个code和reason,而hyperf服务器的onclose中如果获取到这个值呢

如果无法获取,我在swoole的配置中看到了这个,似乎可以变相的获取到,但是当我在server.php的settings中增加了这个配置,似乎并不生效,close并没有回调到onmessage里,是hyperf不支持这个配置嘛

https://wiki.swoole.com/zh-cn/#/server/setting?id=open_websocket_close_frame

但是当我在server.php的settings中增加了这个配置

配置文件发一下。

我看了一下源码。有这个帧,不过WebSocket Server封装时候没有把这个帧返回给回调,你可以通过修改class_map或者其他方式,改一下看看。
image

你的这个文件是当server是协程时才会启动,而我的项目是'mode' => SWOOLE_PROCESS,

配置文件如下
[
'mode' => SWOOLE_PROCESS,
'servers' => [
[
'name' => 'ws',
'type' => ServerInterface::SERVER_WEBSOCKET,
'host' => '0.0.0.0',
'port' => (int)env('WS_PORT'),
'sock_type' => SWOOLE_SOCK_TCP,
'callbacks' => [
Event::ON_HAND_SHAKE => [Hyperf\WebSocketServer\Server::class, 'onHandShake'],
Event::ON_MESSAGE => [Hyperf\WebSocketServer\Server::class, 'onMessage'],
Event::ON_CLOSE => [Hyperf\WebSocketServer\Server::class, 'onClose'],
],
'settings' => [
Constant::OPTION_OPEN_WEBSOCKET_CLOSE_FRAME => true,
]
]
],
'settings' => [
Constant::OPTION_ENABLE_COROUTINE => true,
Constant::OPTION_WORKER_NUM => isOnlyStartAdmin() ? 2 : (int)env('OPTION_WORKER_NUM', swoole_cpu_num()),
Constant::OPTION_PID_FILE => BASE_PATH . '/runtime/hyperf.pid',
Constant::OPTION_OPEN_TCP_NODELAY => true,
Constant::OPTION_MAX_COROUTINE => (int)env('MAX_COROUTINE', 100000),
Constant::OPTION_OPEN_HTTP2_PROTOCOL => true,
Constant::OPTION_MAX_REQUEST => (int)env('OPTION_MAX_REQUEST', 0),
Constant::OPTION_SOCKET_BUFFER_SIZE => 2 * 1024 * 1024,
Constant::OPTION_BUFFER_OUTPUT_SIZE => 2 * 1024 * 1024,
Constant::OPTION_HEARTBEAT_CHECK_INTERVAL => 2 * 60,
Constant::OPTION_HEARTBEAT_IDLE_TIME => 10 * 60,

    Constant::OPTION_DOCUMENT_ROOT => BASE_PATH . '/public',
    Constant::OPTION_ENABLE_STATIC_HANDLER => true,
    Constant::OPTION_DAEMONIZE => (int)env('OPTION_DAEMONIZE', 0),
    Constant::OPTION_OPEN_WEBSOCKET_CLOSE_FRAME => true,
],
'callbacks' => [
    Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
    Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
    Event::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
],

]

我在server里和顶级的settings加了这个配置,并且在启动sever时打印配置已经生效了 并通过swooleServer->set进行了配置 但是onMessage里还是拿不到关闭帧,也就拿不到code了

额,不如你本地先试试?万一呢?

试过了,确实并不走这个逻辑。。