Skip to content

Commit

Permalink
Java 11, plus tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Oct 3, 2020
1 parent da4549f commit 562bf8b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
language: java
jdk:
- oraclejdk8
- openjdk11
cache:
directories:
- "$HOME/.m2"
- "$HOME/.gradle"
- "$HOME/.m2"
- "$HOME/.gradle"
deploy:
provider: heroku
app: http4k-demo-irc
Expand Down
51 changes: 23 additions & 28 deletions src/main/kotlin/org/http4k/demo/IrcApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package org.http4k.demo

import org.http4k.cloudnative.env.Environment
import org.http4k.core.then
import org.http4k.filter.ServerFilters
import org.http4k.routing.ResourceLoader
import org.http4k.filter.ServerFilters.BasicAuth
import org.http4k.routing.ResourceLoader.Companion.Classpath
import org.http4k.routing.bind
import org.http4k.routing.static
import org.http4k.routing.websockets
Expand All @@ -13,35 +13,30 @@ import org.http4k.websocket.WsMessage
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicInteger

object IrcApp {
operator fun invoke(env: Environment): PolyHandler {
val userCounter = AtomicInteger()
val messages = mutableListOf<String>()
val participants = ConcurrentHashMap<String, Websocket>()
fun IrcApp(env: Environment): PolyHandler {
val userCounter = AtomicInteger()
val messages = mutableListOf<String>()
val participants = ConcurrentHashMap<String, Websocket>()

fun addMessage(new: String) {
messages.add(new)
participants.values.forEach { it.send(WsMessage(new)) }
}
fun addMessage(new: String) {
messages.add(new)
participants.values.forEach { it.send(WsMessage(new)) }
}

fun newConnection(ws: Websocket) {
val id = "user${userCounter.incrementAndGet()}"
participants += id to ws
messages.map(::WsMessage).forEach { ws.send(it) }
addMessage("$id joined")
ws.onMessage {
addMessage("$id: ${it.bodyString()}")
}
ws.onClose {
participants -= id
addMessage("$id left")
}
fun newConnection(ws: Websocket) {
val id = "user${userCounter.incrementAndGet()}"
participants += id to ws
messages.map(::WsMessage).forEach { ws.send(it) }
addMessage("$id joined")
ws.onMessage { addMessage("$id: ${it.bodyString()}") }
ws.onClose {
participants -= id
addMessage("$id left")
}
}

val http = ServerFilters.BasicAuth("http4k", env[CREDENTIALS])
.then(static(ResourceLoader.Classpath()))
val ws = websockets("/ws" bind ::newConnection)
val http = BasicAuth("http4k", env[CREDENTIALS]).then(static(Classpath()))
val ws = websockets("/ws" bind ::newConnection)

return PolyHandler(http, ws)
}
return PolyHandler(http, ws)
}

0 comments on commit 562bf8b

Please sign in to comment.