mirror of
https://github.com/codex-storage/nim-websock.git
synced 2025-02-02 13:53:21 +00:00
add autobahn_client for incoming autobahn client test (#34)
This commit is contained in:
parent
eb62ec1725
commit
93f0aba685
76
examples/autobahn_client.nim
Normal file
76
examples/autobahn_client.nim
Normal file
@ -0,0 +1,76 @@
|
||||
import
|
||||
std/[strutils],
|
||||
pkg/[chronos, chronicles, stew/byteutils],
|
||||
../ws/[ws, types, frame]
|
||||
|
||||
type
|
||||
Arg = object
|
||||
host: string
|
||||
port: Port
|
||||
path: string
|
||||
|
||||
proc getCaseCount(arg: Arg): Future[int] {.async.} =
|
||||
let path = arg.path & "/getCaseCount"
|
||||
|
||||
var caseCount = 0
|
||||
block:
|
||||
try:
|
||||
let ws = await WebSocket.connect(arg.host, arg.port, path)
|
||||
|
||||
let buff = await ws.recv()
|
||||
if buff.len <= 0:
|
||||
break
|
||||
|
||||
let dataStr = string.fromBytes(buff)
|
||||
caseCount = parseInt(dataStr)
|
||||
|
||||
await ws.close()
|
||||
break
|
||||
|
||||
except WebSocketError as exc:
|
||||
error "WebSocket error", exception = exc.msg
|
||||
except ValueError as exc:
|
||||
error "ParseInt error", exception = exc.msg
|
||||
|
||||
return caseCount
|
||||
|
||||
proc generateReport(arg: Arg) {.async.} =
|
||||
let path = arg.path & "/updateReports?agent=nim-ws"
|
||||
try:
|
||||
let ws = await WebSocket.connect(arg.host, arg.port, path)
|
||||
|
||||
while true:
|
||||
let buff = await ws.recv()
|
||||
if buff.len <= 0:
|
||||
break
|
||||
|
||||
await ws.close()
|
||||
|
||||
except WebSocketError as exc:
|
||||
error "WebSocket error", exception = exc.msg
|
||||
|
||||
proc main() {.async.} =
|
||||
let arg = Arg(host: "127.0.0.1", port: Port(9001))
|
||||
let caseCount = await getCaseCount(arg)
|
||||
notice "case count", count=caseCount
|
||||
|
||||
for i in 1..caseCount:
|
||||
let path = "$1/runCase?case=$2&agent=nim-ws" % [arg.path, $i]
|
||||
try:
|
||||
let ws = await WebSocket.connect(arg.host, arg.port, path)
|
||||
|
||||
# echo back
|
||||
while true:
|
||||
let data = await ws.recv()
|
||||
if data.len <= 0:
|
||||
break
|
||||
|
||||
await ws.send(data, if ws.binary: Opcode.Binary else: Opcode.Text)
|
||||
|
||||
await ws.close()
|
||||
except WebSocketError as exc:
|
||||
error "WebSocket error", exception = exc.msg
|
||||
|
||||
await generateReport(arg)
|
||||
|
||||
waitFor main()
|
Loading…
x
Reference in New Issue
Block a user