v / vlib / net / websocket / tests / autobahn / local_run / autobahn_client.v
33 lines · 29 sloc · 854 bytes · 017ace6ea7402430a992aa0820d5e472ebca74c7
Raw
1// use this test to test the websocket client in the autobahn test
2module main
3
4import net.websocket
5
6fn main() {
7 for i in 1 .. 304 {
8 println('\ncase: ${i}')
9 handle_case(i) or { println('error should be ok: ${err}') }
10 }
11 // update the reports
12 uri := 'ws://localhost:9001/updateReports?agent=v-client'
13 mut ws := websocket.new_client(uri)!
14 ws.connect()!
15 ws.listen()!
16}
17
18fn handle_case(case_nr int) ! {
19 uri := 'ws://localhost:9001/runCase?case=${case_nr}&agent=v-client'
20 mut ws := websocket.new_client(uri)!
21 ws.on_message(on_message)
22 ws.connect()!
23 ws.listen()!
24}
25
26fn on_message(mut ws websocket.Client, msg &websocket.Message) ! {
27 // autobahn tests expects to send same message back
28 if msg.opcode == .pong {
29 // We just wanna pass text and binary message back to autobahn
30 return
31 }
32 ws.write(msg.payload, msg.opcode) or { panic(err) }
33}
34