有没有办法在android设备上使用ktor WebSocket lib检查connectionState?

wyyhbhjk  于 11个月前  发布在  Android
关注(0)|答案(1)|浏览(125)

有没有办法检查与ktor WebSocket lib的Android设备上的连接状态?我想通知当WebSocket连接或断开连接.这有什么乐趣?
这是我的代码:

try {

        client = HttpClient() {
            install(WebSockets) {
            }
        }
        client.ws(
            method = HttpMethod.Get,
            host = "hostName", path = "/ws"
        ) {

            send(Frame.Text("SampleText"))


                for (frame in incoming) {
                    when (frame) {
                       checkFrame()
                    }

                }
            }

    } catch (e: Exception) {
        Log.i(TAG, "${e.message} Exception")

字符串
}

lnxxn5zx

lnxxn5zx1#

您可以查看是否收到了关闭帧:

when (frame) {
   is Frame.Text ->  // Do text stuff here 
   is Frame.Close -> // Socket is closing/Closed
 }

字符串
或者检查DefaultClientWebSocketSession是否有closeReason:

if(webSocketSession?.closeReason != null) {
  // Socket closed
}

相关问题