Last active
November 20, 2021 09:44
-
-
Save vaibhavgoyal09/6641f75b9f252dc47135c21fdaa96f94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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