Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
doorbash committed Oct 6, 2020
1 parent 29bf30d commit 9b193cd
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/main/java/io/colyseus/example/kotlin/Main3.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.colyseus.example.kotlin

import io.colyseus.Client
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlin.concurrent.thread

object Main3 {

val client = Client("ws://localhost:2567")

@JvmStatic
fun main(args: Array<String>) {
for (i in 0..10) connect(i)
GlobalScope.launch {
for (i in 10..20) connectAsync(i)
}

for (i in 10..20) {
GlobalScope.launch {
connectAsync(i)
}
}
for (i in 20..30) connect(i)

thread { while (true) { } }
}

fun connect(i: Int) {
println("[#$i] connecting...")
client.joinOrCreate(MyState::class.java, "game", callback = { room ->
println("[#$i] connected to ${room.name}")
}, onError = Exception::printStackTrace)
}

suspend fun connectAsync(i: Int) {
println("[#$i] connecting...")
with(client.joinOrCreate(MyState::class.java, "game")) {
println("[#$i] connected to $name")
}
}
}

0 comments on commit 9b193cd

Please sign in to comment.