From f9a873736efd50e8fab116c16f375f5e568314bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20Hellstr=C3=B6m?= Date: Wed, 6 Jan 2021 15:43:54 +0100 Subject: [PATCH] x.websocket: server broadcast plus examples (#7922) --- cmd/tools/modules/testing/common.v | 2 + examples/x/websocket/client-server/client.v | 60 +++++++++++++++++++++ examples/x/websocket/client-server/server.v | 48 +++++++++++++++++ vlib/x/websocket/websocket_client.v | 2 - vlib/x/websocket/websocket_server.v | 5 +- 5 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 examples/x/websocket/client-server/client.v create mode 100644 examples/x/websocket/client-server/server.v diff --git a/cmd/tools/modules/testing/common.v b/cmd/tools/modules/testing/common.v index e225110209..dedb30964b 100644 --- a/cmd/tools/modules/testing/common.v +++ b/cmd/tools/modules/testing/common.v @@ -118,6 +118,8 @@ pub fn new_test_session(_vargs string) TestSession { $if windows { skip_files << 'examples/database/mysql.v' skip_files << 'examples/x/websocket/ping.v' // requires OpenSSL + skip_files << 'examples/x/websocket/client-server/client.v' // requires OpenSSL + skip_files << 'examples/x/websocket/client-server/server.v' // requires OpenSSL } if github_job != 'ubuntu-tcc' { skip_files << 'examples/wkhtmltopdf.v' // needs installation of wkhtmltopdf from https://github.com/wkhtmltopdf/packaging/releases diff --git a/examples/x/websocket/client-server/client.v b/examples/x/websocket/client-server/client.v new file mode 100644 index 0000000000..9fe35675a6 --- /dev/null +++ b/examples/x/websocket/client-server/client.v @@ -0,0 +1,60 @@ +module main + +import os +import x.websocket +import term + +// This client should be compiled an run in different konsoles +// it connects to the server who will broadcast your messages +// to all other connected clients +fn main() { + mut ws := start_client() ? + println(term.green('client $ws.id ready')) + println('Write message and enter to send...') + for { + line := os.get_line() + if line == '' { + break + } + ws.write_str(line) + } + ws.close(1000, 'normal') or { + println(term.red('panicing $err')) + } + unsafe { + ws.free() + } +} + +fn start_client() ?&websocket.Client { + mut ws := websocket.new_client('ws://localhost:30000')? + // mut ws := websocket.new_client('wss://echo.websocket.org:443')? + // use on_open_ref if you want to send any reference object + ws.on_open(fn (mut ws websocket.Client) ? { + println(term.green('websocket connected to the server and ready to send messages...')) + }) + // use on_error_ref if you want to send any reference object + ws.on_error(fn (mut ws websocket.Client, err string) ? { + println(term.red('error: $err')) + }) + // use on_close_ref if you want to send any reference object + ws.on_close(fn (mut ws websocket.Client, code int, reason string) ? { + println(term.green('the connection to the server successfully closed')) + }) + // on new messages from other clients, display them in blue text + ws.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? { + if msg.payload.len > 0 { + message := msg.payload.bytestr() + println(term.blue('$message')) + } + }) + + ws.connect() or { + println(term.red('error on connect: $err')) + } + + go ws.listen() or { + println(term.red('error on listen $err')) + } + return ws +} \ No newline at end of file diff --git a/examples/x/websocket/client-server/server.v b/examples/x/websocket/client-server/server.v new file mode 100644 index 0000000000..6fc253d32b --- /dev/null +++ b/examples/x/websocket/client-server/server.v @@ -0,0 +1,48 @@ +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() +} + +fn start_server() ? { + mut s := websocket.new_server(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 { + mut c := cli + 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() + } +} diff --git a/vlib/x/websocket/websocket_client.v b/vlib/x/websocket/websocket_client.v index e749c41ec5..558afc444c 100644 --- a/vlib/x/websocket/websocket_client.v +++ b/vlib/x/websocket/websocket_client.v @@ -333,11 +333,9 @@ pub fn (mut ws Client) close(code int, message string) ? { close_frame[i + 2] = message[i] } ws.send_control_frame(.close, 'CLOSE', close_frame) ? - ws.send_close_event(code, message) unsafe { close_frame.free() } } else { ws.send_control_frame(.close, 'CLOSE', []) ? - ws.send_close_event(code, '') } ws.fragments = [] } diff --git a/vlib/x/websocket/websocket_server.v b/vlib/x/websocket/websocket_server.v index 34185c4a73..67bd314f31 100644 --- a/vlib/x/websocket/websocket_server.v +++ b/vlib/x/websocket/websocket_server.v @@ -10,7 +10,6 @@ import rand // Server represents a websocket server connection pub struct Server { mut: - clients map[string]&ServerClient // clients connected to this server logger &log.Log // logger used to log ls net.TcpListener // listener used to get incoming connection to socket accept_client_callbacks []AcceptClientFn // accept client callback functions @@ -20,8 +19,8 @@ pub: port int // port used as listen to incoming connections is_ssl bool // true if secure connection (not supported yet on server) pub mut: - ping_interval int = 30 - // interval for sending ping to clients (seconds) + clients map[string]&ServerClient // clients connected to this server + ping_interval int = 30 // interval for sending ping to clients (seconds) state State // current state of connection }