|
// retoor <retoor@molodetz.nl>
|
|
|
|
import "websocket" for WebSocket, WebSocketServer
|
|
import "scheduler" for Scheduler
|
|
import "timer" for Timer
|
|
|
|
var port = 8080
|
|
var clientCount = 0
|
|
var messageCount = 0
|
|
|
|
System.print("=== Concurrent WebSocket Server ===")
|
|
System.print("Listening on ws://0.0.0.0:%(port)")
|
|
System.print("Each client connection runs in its own fiber.\n")
|
|
|
|
var server = WebSocketServer.bind("0.0.0.0", port)
|
|
|
|
var handleClient = Fn.new { |ws, clientId|
|
|
System.print("[Client %(clientId)] Connected")
|
|
|
|
while (ws.isOpen) {
|
|
var msg = ws.receive()
|
|
|
|
if (msg == null) {
|
|
System.print("[Client %(clientId)] Connection lost")
|
|
break
|
|
}
|
|
|
|
if (msg.isClose) {
|
|
System.print("[Client %(clientId)] Sent close frame")
|
|
break
|
|
}
|
|
|
|
if (msg.isText) {
|
|
messageCount = messageCount + 1
|
|
var text = msg.text
|
|
System.print("[Client %(clientId)] Received: %(text) (total messages: %(messageCount))")
|
|
|
|
if (text == "ping") {
|
|
ws.send("pong")
|
|
} else if (text == "stats") {
|
|
ws.send("clients: %(clientCount), messages: %(messageCount)")
|
|
} else if (text == "broadcast") {
|
|
ws.send("broadcast not implemented in this example")
|
|
} else {
|
|
ws.send("echo: %(text)")
|
|
}
|
|
} else if (msg.isBinary) {
|
|
messageCount = messageCount + 1
|
|
System.print("[Client %(clientId)] Received binary: %(msg.bytes.count) bytes")
|
|
ws.sendBinary(msg.bytes)
|
|
}
|
|
}
|
|
|
|
clientCount = clientCount - 1
|
|
System.print("[Client %(clientId)] Disconnected (%(clientCount) clients remaining)")
|
|
}
|
|
|
|
while (true) {
|
|
var ws = server.accept()
|
|
|
|
if (ws != null) {
|
|
clientCount = clientCount + 1
|
|
var clientId = clientCount
|
|
|
|
Scheduler.add {
|
|
handleClient.call(ws, clientId)
|
|
}
|
|
}
|
|
}
|