作为http4s中的服务器,如何确定websocket连接是否已关闭?

时间:2015-07-29 23:18:44

标签: scala websocket server

这是试图找出在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。服务器如何知道客户端是否请求连接关闭?

0 个答案:

没有答案