Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
caijwei opened this issue Apr 29, 2024 · 4 comments
Open
Labels
question Further information is requested

Comments

@caijwei
Copy link

caijwei commented Apr 29, 2024

按照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

@caijwei caijwei added the question Further information is requested label Apr 29, 2024
@lazychanger
Copy link
Contributor

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

配置文件发一下。

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

@caijwei
Copy link
Author

caijwei commented Apr 29, 2024

你的这个文件是当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了

@lazychanger
Copy link
Contributor

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

@caijwei
Copy link
Author

caijwei commented May 11, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants