v2 / vlib / net / websocket / tests / autobahn / autobahn_server.v
27 lines · 23 sloc · 688 bytes · 017ace6ea7402430a992aa0820d5e472ebca74c7
Raw
1// use this to test websocket server to the autobahn test
2module main
3
4import net.websocket
5
6fn main() {
7 mut s := websocket.new_server(.ip6, 9002, '/')
8 s.on_message(on_message)
9 s.listen() or { panic(err) }
10}
11
12fn handle_case(case_nr int) ! {
13 uri := 'ws://localhost:9002/runCase?case=${case_nr}&agent=v-client'
14 mut ws := websocket.new_client(uri)!
15 ws.on_message(on_message)
16 ws.connect()!
17 ws.listen()!
18}
19
20fn on_message(mut ws websocket.Client, msg &websocket.Message) ! {
21 // autobahn tests expects to send same message back
22 if msg.opcode == .pong {
23 // We just wanna pass text and binary message back to autobahn
24 return
25 }
26 ws.write(msg.payload, msg.opcode) or { panic(err) }
27}
28