Skip to content

Instantly share code, notes, and snippets.

@vaibhavgoyal09
Last active November 20, 2021 09:44
Show Gist options
  • Save vaibhavgoyal09/6641f75b9f252dc47135c21fdaa96f94 to your computer and use it in GitHub Desktop.
Save vaibhavgoyal09/6641f75b9f252dc47135c21fdaa96f94 to your computer and use it in GitHub Desktop.
webSocket {
try {
incoming.consumeEach { frame ->
if (frame is Frame.Text) {
val frameTextReceived = frame.readText()
val jsonObject = JsonParser.parseString(frameTextReceived).asJsonObject
val type = when (jsonObject.get("type").asString) {
TYPE_GAME_MOVE -> GameMove::class.java
TYPE_DISCONNECT_REQUEST -> DisconnectRequest::class.java
else -> BaseModel::class.java
}
val message = gson.fromJson(frameTextReceived, type)
when(message) {
is GameMove -> // Handle Game Move
is DisconnectRequest -> // Handle disconnect request
}
}
}
} catch (e: Exception) {
e.printStackTrace()
} finally {
// Handle Socket Closed
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment