From c91f7746fdf3541c2d653a57c50aa6484e8b54bd Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 31 Jan 2021 15:18:04 +0100 Subject: [PATCH] websocket: minor improvements in the server example --- examples/x/websocket/client-server/server.v | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/examples/x/websocket/client-server/server.v b/examples/x/websocket/client-server/server.v index 68dceb96b9..154d2d44e5 100644 --- a/examples/x/websocket/client-server/server.v +++ b/examples/x/websocket/client-server/server.v @@ -1,14 +1,12 @@ module main -import os import x.websocket import term // this server accepts client connections and broadcast all messages to other connected clients fn main() { - go start_server() - println('press enter to quit...') - os.get_line() + println('press ctrl-c to quit...') + start_server() ? } fn start_server() ? { @@ -22,17 +20,15 @@ fn start_server() ? { return false } return true - })? - + }) ? + // on_message_ref, broadcast all incoming messages to all clients except the one sent it s.on_message_ref(fn (mut ws websocket.Client, msg &websocket.Message, mut m websocket.Server) ? { // for _, cli in m.clients { for i, _ in m.clients { mut c := m.clients[i] - if c.client.state == .open && c.client.id != ws.id { - c.client.write(msg.payload, websocket.OPCode.text_frame) or { - panic(err) - } + if c.client.state == .open && c.client.id != ws.id { + c.client.write(msg.payload, websocket.OPCode.text_frame) or { panic(err) } } } }, s) @@ -40,9 +36,7 @@ fn start_server() ? { s.on_close(fn (mut ws websocket.Client, code int, reason string) ? { println(term.green('client ($ws.id) closed connection')) }) - s.listen() or { - println(term.red('error on server listen: $err')) - } + s.listen() or { println(term.red('error on server listen: $err')) } unsafe { s.free() }