|
| 1 | +""" |
| 2 | +A multi-client chat server that broadcasts messages to all connected clients. |
| 3 | +
|
| 4 | +Demonstrates inter-actor communication: `ChatHandler` actors register with |
| 5 | +`ChatListener`, which maintains a set of connected handlers. When a client |
| 6 | +sends a message, its handler asks the listener to broadcast to all other |
| 7 | +handlers. |
| 8 | +
|
| 9 | +Connect multiple clients with: `websocat ws://localhost:8083` |
| 10 | +""" |
| 11 | +use collections = "collections" |
| 12 | +use lori = "lori" |
| 13 | +use ws = "../../mare" |
| 14 | + |
| 15 | +actor Main |
| 16 | + new create(env: Env) => |
| 17 | + let auth = lori.TCPListenAuth(env.root) |
| 18 | + let config = ws.WebSocketConfig(where |
| 19 | + host' = "localhost", |
| 20 | + port' = "8083") |
| 21 | + ChatListener(auth, config, env.out) |
| 22 | + |
| 23 | +actor ChatListener is lori.TCPListenerActor |
| 24 | + var _tcp_listener: lori.TCPListener = lori.TCPListener.none() |
| 25 | + let _server_auth: lori.TCPServerAuth |
| 26 | + let _config: ws.WebSocketConfig val |
| 27 | + let _out: OutStream |
| 28 | + let _handlers: collections.SetIs[ChatHandler tag] = collections.SetIs[ChatHandler tag] |
| 29 | + |
| 30 | + new create( |
| 31 | + auth: lori.TCPListenAuth, |
| 32 | + config: ws.WebSocketConfig val, |
| 33 | + out: OutStream) |
| 34 | + => |
| 35 | + _server_auth = lori.TCPServerAuth(auth) |
| 36 | + _config = config |
| 37 | + _out = out |
| 38 | + _tcp_listener = lori.TCPListener(auth, config.host, config.port, this) |
| 39 | + |
| 40 | + fun ref _listener(): lori.TCPListener => _tcp_listener |
| 41 | + |
| 42 | + fun ref _on_accept(fd: U32): ChatHandler => |
| 43 | + ChatHandler(_server_auth, fd, _config, _out, this) |
| 44 | + |
| 45 | + fun ref _on_listening() => |
| 46 | + _out.print("Listening on " + _config.host + ":" + _config.port) |
| 47 | + |
| 48 | + fun ref _on_listen_failure() => |
| 49 | + _out.print("Failed to listen on " + _config.host + ":" + _config.port) |
| 50 | + |
| 51 | + be register(handler: ChatHandler tag) => |
| 52 | + _handlers.set(handler) |
| 53 | + _out.print("Client joined (" + _handlers.size().string() + " connected)") |
| 54 | + |
| 55 | + be deregister(handler: ChatHandler tag) => |
| 56 | + _handlers.unset(handler) |
| 57 | + _out.print("Client left (" + _handlers.size().string() + " connected)") |
| 58 | + |
| 59 | + be broadcast(sender: ChatHandler tag, data: String val) => |
| 60 | + for handler in _handlers.values() do |
| 61 | + if handler isnt sender then |
| 62 | + handler.deliver(data) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | +actor ChatHandler is ws.WebSocketServerActor |
| 67 | + var _ws: ws.WebSocketServer = ws.WebSocketServer.none() |
| 68 | + let _out: OutStream |
| 69 | + let _listener_tag: ChatListener tag |
| 70 | + |
| 71 | + new create( |
| 72 | + auth: lori.TCPServerAuth, |
| 73 | + fd: U32, |
| 74 | + config: ws.WebSocketConfig val, |
| 75 | + out: OutStream, |
| 76 | + listener: ChatListener tag) |
| 77 | + => |
| 78 | + _out = out |
| 79 | + _listener_tag = listener |
| 80 | + _ws = ws.WebSocketServer(auth, fd, this, config) |
| 81 | + |
| 82 | + fun ref _websocket(): ws.WebSocketServer => _ws |
| 83 | + |
| 84 | + fun ref on_open(request: ws.UpgradeRequest val) => |
| 85 | + _listener_tag.register(this) |
| 86 | + |
| 87 | + fun ref on_text_message(data: String val) => |
| 88 | + _listener_tag.broadcast(this, data) |
| 89 | + |
| 90 | + fun ref on_closed( |
| 91 | + close_status: ws.CloseStatus, |
| 92 | + close_reason: String val) |
| 93 | + => |
| 94 | + _listener_tag.deregister(this) |
| 95 | + |
| 96 | + be deliver(data: String val) => |
| 97 | + _ws.send_text(data) |
0 commit comments