当websocket服务器';s的以太网电缆已拔下
我正在Android 8中使用Ktor 1.2.5和Kotlin 1.3.61开发网络应用程序 当手动关闭netty websocket服务器的wifi(或手动拔下以太网电缆)时,即使发送了某些信息,也不会在相反的websocket客户端中发生任何事件或异常。这意味着websocket客户端不知道会话丢失 相反,当websocket客户端的wifi关闭时,Web服务器可以捕获它,因为closedException:ClosedSendChannelException在发送内容时发生 当服务器的网络断开连接时,websocket客户端是否有办法知道“会话丢失”? (当webserver的网络断开连接时,websocket客户端甚至无法正常关闭——换句话说,无法调用最后一个块- 这是代码当websocket服务器';s的以太网电缆已拔下,websocket,ktor,Websocket,Ktor,我正在Android 8中使用Ktor 1.2.5和Kotlin 1.3.61开发网络应用程序 当手动关闭netty websocket服务器的wifi(或手动拔下以太网电缆)时,即使发送了某些信息,也不会在相反的websocket客户端中发生任何事件或异常。这意味着websocket客户端不知道会话丢失 相反,当websocket客户端的wifi关闭时,Web服务器可以捕获它,因为closedException:ClosedSendChannelException在发送内容时发生 当服务器的网
-----------------------------------------------------------------
SERVER
-----------------------------------------------------------------
embeddedServer(Netty, port = 8080) {
install(DefaultHeaders)
install(CallLogging)
install(WebSockets) {
pingPeriod = Duration.ofSeconds(10)
timeout = Duration.ofSeconds(5)
}
...
routing {
webSocket("/test") {
try {
incoming.consumeEach { frame ->
if(frame is Frame.Text) {
Log.d("test", frame.readText())
}
}
} finally {
val reason: CloseReason? = closeReason.await()
Log.d("test", reason.toString())
}
}
}
}
private suspend fun sendSomething(data:String) {
try {
clients.send(Frame.Binary(true, data))
}
catch (closedException: ClosedSendChannelException) {
Log.d("test", "Server ClosedSendChannelException is happened")
}
}
-----------------------------------------------------------------
CLIENT
-----------------------------------------------------------------
CoroutineScope(Dispatchers.IO).launch {
try {
client.ws(host = ip, port = 8080, path = "/test") {
pingIntervalMillis = 1000*10
timeoutMillis = 1000*5
...
try {
incoming.consumeEach { frame ->
if(frame is Frame.Text) {
Log.d("test", frame.readText())
}
}
} catch (e: ClosedReceiveChannelException) {
Log.d("test", "ClosedSendChannelException is happened")
} catch (e: Throwable) {
Log.d("test", "ClosedSendChannelException is happened")
} finally {
// This block cannot be called even websocket client run explicitly disconnect function, when webserver's Ethernet cable is unplugged.
val reason: CloseReason? = closeReason.await()
Log.d("test", reason.toString())
}
}
} catch (e: Exception) {
if (e is NoRouteToHostException || e is ConnectException) {
Log.d("test", "Exception")
} else if (e.javaClass.`package`?.name.toString().startsWith("java.net")) {
Log.d("test", "java.net")
} else {
Log.d("test", "unknown")
}
}
}
private suspend fun sendSomething(data:String) {
try {
clients.send(Frame.Binary(true, data))
}
catch (closedException: ClosedSendChannelException) {
Log.d("test", "Client ClosedSendChannelException is happened")
}
}