Skip to content

Instantly share code, notes, and snippets.

View vaibhavgoyal09's full-sized avatar
:octocat:
Learning by building stuff

Vaibhav Goyal vaibhavgoyal09

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