forked from jponge/golo-devoxxfr13-demos
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo-12.golo
42 lines (35 loc) · 980 Bytes
/
demo-12.golo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module demo12
import java.net
import java.lang.Thread
import java.util.concurrent
import gololang.concurrent.workers.WorkerEnvironment
local function clientPusher = |socket, msg| {
let out = socket: getOutputStream()
out: write(msg: getBytes())
out: flush()
}
local function broadcast = |clients, msg| {
foreach (client in clients) {
client: port(): send(msg)
}
}
function main = |args| {
let env = WorkerEnvironment.builder(): withCachedThreadPool()
let clients = ConcurrentLinkedQueue()
let broadcastPort = env: spawn(^broadcast: bindTo(clients))
let loopPort = env: spawn(|go| {
while true {
sleep(2000_L)
broadcastPort: send("[tick] ")
}
})
let server = ServerSocket(6666)
loopPort: send("Go!")
while true {
let socket = server: accept()
socket: setSoTimeout(50)
let port = env: spawn(^clientPusher: bindTo(socket))
port: send("Hello!\n")
clients: add(DynamicObject(): port(port): freeze())
}
}