mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
x.websockets: new websockets module on top of x.net (#6189)
This commit is contained in:
73
vlib/x/websocket/websocket_test.v
Normal file
73
vlib/x/websocket/websocket_test.v
Normal file
@ -0,0 +1,73 @@
|
||||
import x.websocket
|
||||
import time
|
||||
|
||||
// Tests with external ws & wss servers
|
||||
fn test_ws() ? {
|
||||
go start_server()
|
||||
time.sleep_ms(100)
|
||||
ws_test('ws://localhost:30000')?
|
||||
}
|
||||
|
||||
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
|
||||
})?
|
||||
s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
|
||||
// payload := if msg.payload.len == 0 { '' } else { string(msg.payload, msg.payload.len) }
|
||||
// println('server client ($ws.id) got message: opcode: $msg.opcode, payload: $payload')
|
||||
ws.write(msg.payload, msg.opcode) or {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
s.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
|
||||
// println('client ($ws.id) closed connection')
|
||||
})
|
||||
s.listen() or {
|
||||
// println('error on server listen: $err')
|
||||
}
|
||||
}
|
||||
|
||||
fn ws_test(uri string) ? {
|
||||
eprintln('connecting to $uri ...')
|
||||
mut ws := websocket.new_client(uri)?
|
||||
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 {
|
||||
println('Message: ${string(msg.payload, msg.payload.len)}')
|
||||
assert string(msg.payload, msg.payload.len) == 'a'
|
||||
} else {
|
||||
println('Binary message: $msg')
|
||||
}
|
||||
})
|
||||
ws.connect()
|
||||
go ws.listen()
|
||||
text := ['a'].repeat(2)
|
||||
for msg in text {
|
||||
ws.write(msg.bytes(), .text_frame)?
|
||||
// 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)
|
||||
}
|
Reference in New Issue
Block a user