照常添加适用的地方
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");
}
...
}
这是我的想法:
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