From f5c4671bfbad96bf346bd7e9a21fc4317b4959df Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Sat, 3 Dec 2022 17:00:20 +0530 Subject: Adds most of the tools --- .../v/examples/websocket/client-server/server.v | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 v_windows/v/examples/websocket/client-server/server.v (limited to 'v_windows/v/examples/websocket/client-server/server.v') diff --git a/v_windows/v/examples/websocket/client-server/server.v b/v_windows/v/examples/websocket/client-server/server.v new file mode 100644 index 0000000..db41913 --- /dev/null +++ b/v_windows/v/examples/websocket/client-server/server.v @@ -0,0 +1,43 @@ +module main + +import net.websocket +import term + +// this server accepts client connections and broadcast all messages to other connected clients +fn main() { + println('press ctrl-c to quit...') + start_server() ? +} + +fn start_server() ? { + mut s := websocket.new_server(.ip6, 30000, '') + // Make that in execution test time give time to execute at least one time + s.ping_interval = 100 + s.on_connect(fn (mut s websocket.ServerClient) ?bool { + // Here you can look att the client info and accept or not accept + // just returning a true/false + if s.resource_name != '/' { + 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) } + } + } + }, s) + + 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')) } + unsafe { + s.free() + } +} -- cgit v1.2.3