1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

examples: improve the output of the websocket examples

This commit is contained in:
Delyan Angelov 2023-01-15 14:48:52 +02:00
parent 634f596d27
commit 7f3531077d
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 93 additions and 29 deletions

View File

@ -9,6 +9,11 @@ import term
// to all other connected clients // to all other connected clients
fn main() { fn main() {
mut ws := start_client()! mut ws := start_client()!
defer {
unsafe {
ws.free()
}
}
println(term.green('client ${ws.id} ready')) println(term.green('client ${ws.id} ready'))
println('Write message and enter to send...') println('Write message and enter to send...')
for { for {
@ -18,9 +23,9 @@ fn main() {
} }
ws.write_string(line)! ws.write_string(line)!
} }
ws.close(1000, 'normal') or { println(term.red('panicing ${err}')) } ws.close(1000, 'normal') or {
unsafe { eprintln(term.red('ws.close err: ${err}'))
ws.free() exit(1)
} }
} }
@ -29,25 +34,28 @@ fn start_client() !&websocket.Client {
// mut ws := websocket.new_client('wss://echo.websocket.org:443')? // mut ws := websocket.new_client('wss://echo.websocket.org:443')?
// use on_open_ref if you want to send any reference object // use on_open_ref if you want to send any reference object
ws.on_open(fn (mut ws websocket.Client) ! { ws.on_open(fn (mut ws websocket.Client) ! {
println(term.green('websocket connected to the server and ready to send messages...')) println(term.green('ws.on_open websocket connected to the server and ready to send messages...'))
}) })
// use on_error_ref if you want to send any reference object // use on_error_ref if you want to send any reference object
ws.on_error(fn (mut ws websocket.Client, err string) ! { ws.on_error(fn (mut ws websocket.Client, err string) ! {
println(term.red('error: ${err}')) println(term.red('ws.on_error error: ${err}'))
}) })
// use on_close_ref if you want to send any reference object // use on_close_ref if you want to send any reference object
ws.on_close(fn (mut ws websocket.Client, code int, reason string) ! { ws.on_close(fn (mut ws websocket.Client, code int, reason string) ! {
println(term.green('the connection to the server successfully closed')) println(term.green('ws.on_close the connection to the server successfully closed'))
}) })
// on new messages from other clients, display them in blue text // on new messages from other clients, display them in blue text
ws.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ! { ws.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ! {
if msg.payload.len > 0 { if msg.payload.len > 0 {
message := msg.payload.bytestr() message := msg.payload.bytestr()
println(term.blue('${message}')) println(term.blue('ws.on_message `${message}`'))
} }
}) })
ws.connect() or { println(term.red('error on connect: ${err}')) } ws.connect() or {
eprintln(term.red('ws.connect error: ${err}'))
return err
}
spawn ws.listen() // or { println(term.red('error on listen $err')) } spawn ws.listen() // or { println(term.red('error on listen $err')) }
return ws return ws

View File

@ -3,17 +3,28 @@ module main
import net.websocket import net.websocket
import term import term
fn slog(message string) {
eprintln(term.colorize(term.bright_yellow, message))
}
// this server accepts client connections and broadcast all messages to other connected clients // this server accepts client connections and broadcast all messages to other connected clients
fn main() { fn main() {
println('press ctrl-c to quit...') eprintln('press ctrl-c to quit...')
start_server()! start_server()!
} }
fn start_server() ! { fn start_server() ! {
slog('start_server')
mut s := websocket.new_server(.ip6, 30000, '') mut s := websocket.new_server(.ip6, 30000, '')
defer {
unsafe {
s.free()
}
}
// Make that in execution test time give time to execute at least one time // Make that in execution test time give time to execute at least one time
s.ping_interval = 100 s.ping_interval = 100
s.on_connect(fn (mut s websocket.ServerClient) !bool { s.on_connect(fn (mut s websocket.ServerClient) !bool {
slog('s.on_connect')
// Here you can look att the client info and accept or not accept // Here you can look att the client info and accept or not accept
// just returning a true/false // just returning a true/false
if s.resource_name != '/' { if s.resource_name != '/' {
@ -24,6 +35,7 @@ fn start_server() ! {
// on_message_ref, broadcast all incoming messages to all clients except the one sent it // 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) ! { s.on_message_ref(fn (mut ws websocket.Client, msg &websocket.Message, mut m websocket.Server) ! {
slog('s.on_message_ref')
// for _, cli in m.clients { // for _, cli in m.clients {
for i, _ in m.clients { for i, _ in m.clients {
mut c := m.clients[i] mut c := m.clients[i]
@ -34,10 +46,10 @@ fn start_server() ! {
}, s) }, s)
s.on_close(fn (mut ws websocket.Client, code int, reason string) ! { s.on_close(fn (mut ws websocket.Client, code int, reason string) ! {
slog('s.on_close')
println(term.green('client (${ws.id}) closed connection')) 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 { slog('s.listen done')
s.free()
}
} }

View File

@ -1,6 +1,7 @@
module main module main
import time import time
import term
import net.websocket import net.websocket
fn main() { fn main() {
@ -9,13 +10,31 @@ fn main() {
start_client()! start_client()!
} }
fn slog(message string) {
eprintln(term.colorize(term.bright_yellow, message))
}
fn clog(message string) {
eprintln(term.colorize(term.cyan, message))
}
fn wlog(message string) {
eprintln(term.colorize(term.bright_blue, message))
}
// start_server starts the websocket server, it receives messages // start_server starts the websocket server, it receives messages
// and send it back to the client that sent it // and send it back to the client that sent it
fn start_server() ! { fn start_server() ! {
mut s := websocket.new_server(.ip6, 30000, '') mut s := websocket.new_server(.ip6, 30000, '')
defer {
unsafe {
s.free()
}
}
// Make that in execution test time give time to execute at least one time // Make that in execution test time give time to execute at least one time
s.ping_interval = 100 s.ping_interval = 100
s.on_connect(fn (mut s websocket.ServerClient) !bool { s.on_connect(fn (mut s websocket.ServerClient) !bool {
slog('ws.on_connect, s.client_key: ${s.client_key}')
// Here you can look att the client info and accept or not accept // Here you can look att the client info and accept or not accept
// just returning a true/false // just returning a true/false
if s.resource_name != '/' { if s.resource_name != '/' {
@ -24,60 +43,85 @@ fn start_server() ! {
return true return true
})! })!
s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ! { s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ! {
ws.write(msg.payload, msg.opcode) or { panic(err) } slog('s.on_message msg.opcode: ${msg.opcode} | msg.payload: ${msg.payload}')
ws.write(msg.payload, msg.opcode) or {
eprintln('ws.write err: ${err}')
return err
}
}) })
s.on_close(fn (mut ws websocket.Client, code int, reason string) ! { s.on_close(fn (mut ws websocket.Client, code int, reason string) ! {
slog('s.on_close code: ${code}, reason: ${reason}')
// println('client ($ws.id) closed connection') // println('client ($ws.id) closed connection')
}) })
s.listen() or { println('error on server listen: ${err}') } s.listen() or {
unsafe { slog('s.listen err: ${err}')
s.free() return err
} }
slog('s.listen finished')
} }
// start_client starts the websocket client, it writes a message to // start_client starts the websocket client, it writes a message to
// the server and prints all the messages received // the server and prints all the messages received
fn start_client() ! { fn start_client() ! {
mut ws := websocket.new_client('ws://localhost:30000')! mut ws := websocket.new_client('ws://localhost:30000')!
defer {
unsafe {
ws.free()
}
}
// mut ws := websocket.new_client('wss://echo.websocket.org:443')? // mut ws := websocket.new_client('wss://echo.websocket.org:443')?
// use on_open_ref if you want to send any reference object // use on_open_ref if you want to send any reference object
ws.on_open(fn (mut ws websocket.Client) ! { ws.on_open(fn (mut ws websocket.Client) ! {
println('open!') clog('ws.on_open')
}) })
// use on_error_ref if you want to send any reference object // use on_error_ref if you want to send any reference object
ws.on_error(fn (mut ws websocket.Client, err string) ! { ws.on_error(fn (mut ws websocket.Client, err string) ! {
println('error: ${err}') clog('ws.on_error error: ${err}')
}) })
// use on_close_ref if you want to send any reference object // use on_close_ref if you want to send any reference object
ws.on_close(fn (mut ws websocket.Client, code int, reason string) ! { ws.on_close(fn (mut ws websocket.Client, code int, reason string) ! {
println('closed') clog('ws.on_close')
}) })
// use on_message_ref if you want to send any reference object // use on_message_ref if you want to send any reference object
ws.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ! { ws.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ! {
if msg.payload.len > 0 { if msg.payload.len > 0 {
message := msg.payload.bytestr() message := msg.payload.bytestr()
println('client got type: ${msg.opcode} payload:\n${message}') clog('ws.on_message client got type: ${msg.opcode} payload: `${message}`')
} }
}) })
// you can add any pointer reference to use in callback // you can add any pointer reference to use in callback
// t := TestRef{count: 10} // t := TestRef{count: 10}
// ws.on_message_ref(fn (mut ws websocket.Client, msg &websocket.Message, r &SomeRef) ? { // ws.on_message_ref(fn (mut ws websocket.Client, msg &websocket.Message, r &SomeRef) ? {
// // println('type: $msg.opcode payload:\n$msg.payload ref: $r') // // eprintln('type: $msg.opcode payload:\n$msg.payload ref: $r')
// }, &r) // }, &r)
ws.connect() or { println('error on connect: ${err}') } ws.connect() or {
spawn write_echo(mut ws) // or { println('error on write_echo $err') } clog('ws.connect err: ${err}')
ws.listen() or { println('error on listen ${err}') } return err
unsafe {
ws.free()
} }
clog('ws.connect succeeded')
spawn write_echo(mut ws) // or { println('error on write_echo $err') }
ws.listen() or {
clog('ws.listen err: ${err}')
return err
}
clog('ws.listen finished')
} }
fn write_echo(mut ws websocket.Client) ! { fn write_echo(mut ws websocket.Client) ! {
wlog('write_echo, start')
message := 'echo this' message := 'echo this'
for i := 0; i <= 10; i++ { for i := 0; i <= 5; i++ {
// Server will send pings every 30 seconds // Server will send pings every 30 seconds
ws.write_string(message) or { println('panicing writing ${err}') } wlog('write_echo, writing message: `${message}` ...')
ws.write_string(message) or {
wlog('write_echo, ws.write_string err: ${err}')
return err
}
time.sleep(100 * time.millisecond) time.sleep(100 * time.millisecond)
} }
ws.close(1000, 'normal') or { println('panicing ${err}') } ws.close(1000, 'normal') or {
wlog('write_echo, close err: ${err}')
return err
}
wlog('write_echo, done')
} }