This file contains hidden or 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
| client.webSocket(urlString = TIC_TAC_TOE_SOCKET_API_URL) { | |
| } |
This file contains hidden or 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
| val client = HttpClient(OkHttp) { | |
| install(JsonFeature) { | |
| serializer = GsonSerializer() | |
| } | |
| install(WebSockets) { | |
| } | |
| } |
This file contains hidden or 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
| plugins { | |
| id 'com.android.application' | |
| id 'kotlin-android' | |
| id 'kotlin-kapt' | |
| id 'kotlin-parcelize' | |
| id 'dagger.hilt.android.plugin' | |
| } | |
| android { | |
| compileSdk 31 |
This file contains hidden or 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
| buildscript { | |
| ext { | |
| compose_version = '1.0.4' | |
| ktorVersion = "1.6.5" | |
| } | |
| repositories { | |
| google() | |
| mavenCentral() | |
| } |
This file contains hidden or 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
| fun Route.gameSocketRoute() { | |
| route("/v1/game") { | |
| standardWebSocket { socket, clientId, _, payload -> | |
| when (payload) { | |
| is GameMove -> { | |
| socket.send(Frame.Text("Game move received ${payload.position}")) | |
| } | |
| is DisconnectRequest -> { | |
| // Do something here | |
| } |
This file contains hidden or 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
| fun Route.gameSocketRoute() { | |
| route("/v1/game") { | |
| standardWebSocket { socket, clientId, _, payload -> | |
| when (payload) { | |
| is GameMove -> { | |
| // Do something here | |
| } | |
| is DisconnectRequest -> { | |
| // Do something here | |
| } |
This file contains hidden or 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 |
This file contains hidden or 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
| abstract class BaseWebSocketMessage(val type: String) |
This file contains hidden or 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
| val socketConnection = SocketConnection() | |
| fun main() { | |
| embeddedServer(Netty, port = port, host = "0.0.0.0") { | |
| install(CallLogging) | |
| configureSockets() | |
| configureRouting() | |
| configureSerialization() | |
| configureSession() |
This file contains hidden or 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
| class SocketConnection { | |
| val players = ConcurrentHashMap<String, Player>() | |
| fun playerJoined(player: Player) { | |
| players[player.clientId] = player | |
| } | |
| } |
NewerOlder