2020-08-22 01:50:38 +03:00
|
|
|
module main
|
|
|
|
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import x.websocket
|
|
|
|
|
|
|
|
fn main() {
|
2021-02-21 18:05:03 +03:00
|
|
|
println('press enter to quit...\n')
|
2020-08-22 01:50:38 +03:00
|
|
|
go start_server()
|
2021-02-27 20:41:06 +03:00
|
|
|
time.sleep(100 * time.millisecond)
|
2020-08-22 01:50:38 +03:00
|
|
|
go start_client()
|
|
|
|
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
|
2021-02-21 18:05:03 +03:00
|
|
|
}) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
|
2021-03-01 02:18:14 +03:00
|
|
|
ws.write(msg.payload, msg.opcode) or { panic(err) }
|
2020-08-22 01:50:38 +03:00
|
|
|
})
|
|
|
|
s.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
|
|
|
|
// println('client ($ws.id) closed connection')
|
|
|
|
})
|
2021-02-21 18:05:03 +03:00
|
|
|
s.listen() or { println('error on server listen: $err') }
|
2020-08-22 01:50:38 +03:00
|
|
|
unsafe {
|
|
|
|
s.free()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start_client() ? {
|
2021-02-21 18:05:03 +03:00
|
|
|
mut ws := websocket.new_client('ws://localhost:30000') ?
|
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
|
|
|
|
ws.on_open(fn (mut ws websocket.Client) ? {
|
|
|
|
println('open!')
|
|
|
|
})
|
|
|
|
// use on_error_ref if you want to send any reference object
|
|
|
|
ws.on_error(fn (mut ws websocket.Client, err string) ? {
|
|
|
|
println('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('closed')
|
|
|
|
})
|
|
|
|
// use on_message_ref if you want to send any reference object
|
|
|
|
ws.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
|
|
|
|
if msg.payload.len > 0 {
|
2020-08-22 13:29:15 +03:00
|
|
|
message := msg.payload.bytestr()
|
2020-08-22 01:50:38 +03:00
|
|
|
println('client got type: $msg.opcode payload:\n$message')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// you can add any pointer reference to use in callback
|
|
|
|
// t := TestRef{count: 10}
|
|
|
|
// ws.on_message_ref(fn (mut ws websocket.Client, msg &websocket.Message, r &SomeRef)? {
|
|
|
|
// // println('type: $msg.opcode payload:\n$msg.payload ref: $r')
|
|
|
|
// }, &r)
|
2021-02-21 18:05:03 +03:00
|
|
|
ws.connect() or { println('error on connect: $err') }
|
2021-02-27 11:16:55 +03:00
|
|
|
go write_echo(mut ws) // or { println('error on write_echo $err') }
|
2021-02-21 18:05:03 +03:00
|
|
|
ws.listen() or { println('error on listen $err') }
|
2020-08-22 01:50:38 +03:00
|
|
|
unsafe {
|
|
|
|
ws.free()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_echo(mut ws websocket.Client) ? {
|
|
|
|
message := 'echo this'
|
|
|
|
for i := 0; i <= 10; i++ {
|
|
|
|
// Server will send pings every 30 seconds
|
2021-02-21 18:05:03 +03:00
|
|
|
ws.write_str(message) or { println('panicing writing $err') }
|
2021-02-27 20:41:06 +03:00
|
|
|
time.sleep(100 * time.millisecond)
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
2021-02-21 18:05:03 +03:00
|
|
|
ws.close(1000, 'normal') or { println('panicing $err') }
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|