aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/examples/websocket/client-server
diff options
context:
space:
mode:
authorIndrajith K L2022-12-03 17:00:20 +0530
committerIndrajith K L2022-12-03 17:00:20 +0530
commitf5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch)
tree2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/examples/websocket/client-server
downloadcli-tools-windows-master.tar.gz
cli-tools-windows-master.tar.bz2
cli-tools-windows-master.zip
Adds most of the toolsHEADmaster
Diffstat (limited to 'v_windows/v/examples/websocket/client-server')
-rw-r--r--v_windows/v/examples/websocket/client-server/client.v54
-rw-r--r--v_windows/v/examples/websocket/client-server/server.v43
2 files changed, 97 insertions, 0 deletions
diff --git a/v_windows/v/examples/websocket/client-server/client.v b/v_windows/v/examples/websocket/client-server/client.v
new file mode 100644
index 0000000..b039415
--- /dev/null
+++ b/v_windows/v/examples/websocket/client-server/client.v
@@ -0,0 +1,54 @@
+module main
+
+import os
+import net.websocket
+import term
+
+// This client should be compiled an run in different consoles
+// 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_string(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
+}
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()
+ }
+}