// retoor <retoor@molodetz.nl>
import "websocket" for WebSocket
import "scheduler" for Scheduler
import "timer" for Timer
import "io" for Stdin
var host = "ws://127.0.0.1:8080"
System.print("=== WebSocket Chat Client ===")
System.print("Connecting to %(host)...")
var ws = WebSocket.connect(host)
System.print("Connected!")
System.print("Commands: /name <name>, /who, /quit")
System.print("Type a message and press Enter to send.\n")
var running = true
Scheduler.add {
while (running && ws.isOpen) {
var msg = ws.receive()
if (msg == null || msg.isClose) {
running = false
System.print("\nConnection closed.")
break
}
if (msg.isText) {
System.print(msg.text)
}
}
}
Timer.sleep(100)
while (running && ws.isOpen) {
System.write("> ")
var line = Stdin.readLine()
if (line == null || line == "/quit") {
running = false
break
}
if (line.count > 0) {
ws.send(line)
}
}
ws.close()
System.print("Disconnected.")