如何在 Ctrl+C 之后使用 Websocket 优雅地退出 D 程序?

问题描述 投票:0回答:1

照常添加适用的地方

    scope (exit)
    {
        listener.stopListening();
    }

还不足以通过 Ctrl+C 优雅地关闭 vibe.d 程序,因为它也使用像这样实现的 Websocket

routes.get("/ws", handleWebSockets(&handleWebSocketConnection));

routes
URLRouter
的实例,相关处理程序看起来像这样

void handleWebSocketConnection(scope WebSocket socket)
{
    ...

    while (true) {
        ...

        if (!socket.connected) break;

        ...

        socket.send("xxxx");
    }

    ...
}

websocket d vibed
1个回答
0
投票

这是我的想法:

    scope (exit)
    {
        listener.stopListening();
        //
        scope WebSocket socket;
        socket.close;
    }

...这就是 Ctrl+C 之后发生的事情:

[00000000(----) INF] Received signal 2. Shutting down.
[main(----) INF] Stopped to listen for HTTP PS C:\Users\menja\Desktop\vibed\websocket> requests on ::1:8080
[main(----) INF] Stopped to listen for HTTP requests on 127.0.0.1:8080
© www.soinside.com 2019 - 2024. All rights reserved.