2020-08-22 01:50:38 +03:00
|
|
|
module main
|
|
|
|
|
|
|
|
import time
|
2023-01-15 15:48:52 +03:00
|
|
|
import term
|
2021-07-03 02:56:00 +03:00
|
|
|
import net.websocket
|
2020-08-22 01:50:38 +03:00
|
|
|
|
|
|
|
fn main() {
|
2022-11-05 10:46:40 +03:00
|
|
|
spawn start_server()
|
2021-02-27 20:41:06 +03:00
|
|
|
time.sleep(100 * time.millisecond)
|
2022-10-16 09:28:57 +03:00
|
|
|
start_client()!
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-15 15:48:52 +03:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2021-07-03 02:56:00 +03:00
|
|
|
// start_server starts the websocket server, it receives messages
|
|
|
|
// and send it back to the client that sent it
|
2022-10-16 09:28:57 +03:00
|
|
|
fn start_server() ! {
|
2021-06-13 23:53:38 +03:00
|
|
|
mut s := websocket.new_server(.ip6, 30000, '')
|
2023-01-15 15:48:52 +03:00
|
|
|
defer {
|
|
|
|
unsafe {
|
|
|
|
s.free()
|
|
|
|
}
|
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
// Make that in execution test time give time to execute at least one time
|
|
|
|
s.ping_interval = 100
|
2022-10-16 09:28:57 +03:00
|
|
|
s.on_connect(fn (mut s websocket.ServerClient) !bool {
|
2023-01-15 15:48:52 +03:00
|
|
|
slog('ws.on_connect, s.client_key: ${s.client_key}')
|
2020-08-22 01:50:38 +03:00
|
|
|
// 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
|
2022-10-16 09:28:57 +03:00
|
|
|
})!
|
|
|
|
s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ! {
|
2023-01-15 15:48:52 +03:00
|
|
|
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
|
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
})
|
2022-10-16 09:28:57 +03:00
|
|
|
s.on_close(fn (mut ws websocket.Client, code int, reason string) ! {
|
2023-01-15 15:48:52 +03:00
|
|
|
slog('s.on_close code: ${code}, reason: ${reason}')
|
2020-08-22 01:50:38 +03:00
|
|
|
// println('client ($ws.id) closed connection')
|
|
|
|
})
|
2023-01-15 15:48:52 +03:00
|
|
|
s.listen() or {
|
|
|
|
slog('s.listen err: ${err}')
|
|
|
|
return err
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
2023-01-15 15:48:52 +03:00
|
|
|
slog('s.listen finished')
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2021-07-03 02:56:00 +03:00
|
|
|
// start_client starts the websocket client, it writes a message to
|
|
|
|
// the server and prints all the messages received
|
2022-10-16 09:28:57 +03:00
|
|
|
fn start_client() ! {
|
|
|
|
mut ws := websocket.new_client('ws://localhost:30000')!
|
2023-01-15 15:48:52 +03:00
|
|
|
defer {
|
|
|
|
unsafe {
|
|
|
|
ws.free()
|
|
|
|
}
|
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
// mut ws := websocket.new_client('wss://echo.websocket.org:443')?
|
|
|
|
// use on_open_ref if you want to send any reference object
|
2022-10-16 09:28:57 +03:00
|
|
|
ws.on_open(fn (mut ws websocket.Client) ! {
|
2023-01-15 15:48:52 +03:00
|
|
|
clog('ws.on_open')
|
2020-08-22 01:50:38 +03:00
|
|
|
})
|
|
|
|
// use on_error_ref if you want to send any reference object
|
2022-10-16 09:28:57 +03:00
|
|
|
ws.on_error(fn (mut ws websocket.Client, err string) ! {
|
2023-01-15 15:48:52 +03:00
|
|
|
clog('ws.on_error error: ${err}')
|
2020-08-22 01:50:38 +03:00
|
|
|
})
|
|
|
|
// use on_close_ref if you want to send any reference object
|
2022-10-16 09:28:57 +03:00
|
|
|
ws.on_close(fn (mut ws websocket.Client, code int, reason string) ! {
|
2023-01-15 15:48:52 +03:00
|
|
|
clog('ws.on_close')
|
2020-08-22 01:50:38 +03:00
|
|
|
})
|
|
|
|
// use on_message_ref if you want to send any reference object
|
2022-10-16 09:28:57 +03:00
|
|
|
ws.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ! {
|
2020-08-22 01:50:38 +03:00
|
|
|
if msg.payload.len > 0 {
|
2020-08-22 13:29:15 +03:00
|
|
|
message := msg.payload.bytestr()
|
2023-01-15 15:48:52 +03:00
|
|
|
clog('ws.on_message client got type: ${msg.opcode} payload: `${message}`')
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
// you can add any pointer reference to use in callback
|
|
|
|
// t := TestRef{count: 10}
|
2022-05-13 06:56:21 +03:00
|
|
|
// ws.on_message_ref(fn (mut ws websocket.Client, msg &websocket.Message, r &SomeRef) ? {
|
2023-01-15 15:48:52 +03:00
|
|
|
// // eprintln('type: $msg.opcode payload:\n$msg.payload ref: $r')
|
2020-08-22 01:50:38 +03:00
|
|
|
// }, &r)
|
2023-01-15 15:48:52 +03:00
|
|
|
ws.connect() or {
|
|
|
|
clog('ws.connect err: ${err}')
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
clog('ws.connect succeeded')
|
2022-11-05 10:46:40 +03:00
|
|
|
spawn write_echo(mut ws) // or { println('error on write_echo $err') }
|
2023-01-15 15:48:52 +03:00
|
|
|
ws.listen() or {
|
|
|
|
clog('ws.listen err: ${err}')
|
|
|
|
return err
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
2023-01-15 15:48:52 +03:00
|
|
|
clog('ws.listen finished')
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
fn write_echo(mut ws websocket.Client) ! {
|
2023-01-15 15:48:52 +03:00
|
|
|
wlog('write_echo, start')
|
2020-08-22 01:50:38 +03:00
|
|
|
message := 'echo this'
|
2023-01-15 15:48:52 +03:00
|
|
|
for i := 0; i <= 5; i++ {
|
2020-08-22 01:50:38 +03:00
|
|
|
// Server will send pings every 30 seconds
|
2023-01-15 15:48:52 +03:00
|
|
|
wlog('write_echo, writing message: `${message}` ...')
|
|
|
|
ws.write_string(message) or {
|
|
|
|
wlog('write_echo, ws.write_string err: ${err}')
|
|
|
|
return err
|
|
|
|
}
|
2021-02-27 20:41:06 +03:00
|
|
|
time.sleep(100 * time.millisecond)
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
2023-01-15 15:48:52 +03:00
|
|
|
ws.close(1000, 'normal') or {
|
|
|
|
wlog('write_echo, close err: ${err}')
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
wlog('write_echo, done')
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|