这是试图找出在websocket连接关闭时作为服务器注意的最佳方式的尝试:
"Is there a way to know that the connection has closed?" in async {
val receivedClosedFrame = Promise[Unit]
val mock = HttpService {
case req@GET -> Root =>
val sink: Sink[Task, WebSocketFrame] = Process.constant {
case f => Task.delay{receivedClosedFrame.success(())}
}
WS(Exchange(Process.empty, sink))
}
Http4s.withWebSocketService(mock, 9090) { uri =>
for {
conn <- Connection.connect(uri)
_ <- conn.disconnect()
_ <- Task.delay(Await.ready(receivedClosedFrame.future, 3.seconds))
} yield ()
}
}
在上面的示例中,测试超时,因为服务器没有收到WebSocketFrame
。服务器如何知道客户端是否请求连接关闭?