2020-08-22 01:50:38 +03:00
|
|
|
import x.websocket
|
|
|
|
import time
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// tests with internal ws servers
|
2020-11-20 14:32:46 +03:00
|
|
|
fn test_ws() {
|
2020-08-22 01:50:38 +03:00
|
|
|
go start_server()
|
|
|
|
time.sleep_ms(100)
|
2020-11-20 14:32:46 +03:00
|
|
|
ws_test('ws://localhost:30000') or {
|
|
|
|
assert false
|
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn start_server() ? {
|
|
|
|
mut s := websocket.new_server(30000, '')
|
2020-12-04 03:52:26 +03:00
|
|
|
// make that in execution test time give time to execute at least one time
|
2020-08-22 01:50:38 +03:00
|
|
|
s.ping_interval = 100
|
|
|
|
s.on_connect(fn (mut s websocket.ServerClient) ?bool {
|
2020-12-04 03:52:26 +03:00
|
|
|
// here you can look att the client info and accept or not accept
|
2020-08-22 01:50:38 +03:00
|
|
|
// just returning a true/false
|
|
|
|
if s.resource_name != '/' {
|
2020-11-07 19:14:33 +03:00
|
|
|
panic('unexpected resource name in test')
|
2020-08-22 01:50:38 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2020-11-20 14:32:46 +03:00
|
|
|
}) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
|
|
|
|
ws.write(msg.payload, msg.opcode) or {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
s.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
|
2020-12-04 03:52:26 +03:00
|
|
|
// not used
|
2020-08-22 01:50:38 +03:00
|
|
|
})
|
2020-12-04 03:52:26 +03:00
|
|
|
s.listen() or {}
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-04 03:52:26 +03:00
|
|
|
// ws_test tests connect to the websocket server from websocket client
|
2020-08-22 01:50:38 +03:00
|
|
|
fn ws_test(uri string) ? {
|
|
|
|
eprintln('connecting to $uri ...')
|
2020-11-20 14:32:46 +03:00
|
|
|
mut ws := websocket.new_client(uri) ?
|
2020-08-22 01:50:38 +03:00
|
|
|
ws.on_open(fn (mut ws websocket.Client) ? {
|
|
|
|
println('open!')
|
|
|
|
ws.pong()
|
|
|
|
assert true
|
|
|
|
})
|
|
|
|
ws.on_error(fn (mut ws websocket.Client, err string) ? {
|
|
|
|
println('error: $err')
|
|
|
|
// this can be thrown by internet connection problems
|
|
|
|
assert false
|
|
|
|
})
|
|
|
|
ws.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
|
|
|
|
println('closed')
|
|
|
|
})
|
|
|
|
ws.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
|
|
|
|
println('client got type: $msg.opcode payload:\n$msg.payload')
|
|
|
|
if msg.opcode == .text_frame {
|
2020-08-22 13:29:15 +03:00
|
|
|
smessage := msg.payload.bytestr()
|
|
|
|
println('Message: $smessage')
|
|
|
|
assert smessage == 'a'
|
2020-08-22 01:50:38 +03:00
|
|
|
} else {
|
|
|
|
println('Binary message: $msg')
|
|
|
|
}
|
|
|
|
})
|
2020-11-07 19:14:33 +03:00
|
|
|
ws.connect() or {
|
|
|
|
panic('fail to connect')
|
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
go ws.listen()
|
|
|
|
text := ['a'].repeat(2)
|
|
|
|
for msg in text {
|
2020-11-07 19:14:33 +03:00
|
|
|
ws.write(msg.bytes(), .text_frame) or {
|
|
|
|
panic('fail to write to websocket')
|
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
// sleep to give time to recieve response before send a new one
|
|
|
|
time.sleep_ms(100)
|
|
|
|
}
|
|
|
|
// sleep to give time to recieve response before asserts
|
|
|
|
time.sleep_ms(500)
|
|
|
|
}
|