2021-04-05 21:01:10 +00:00
|
|
|
import ../src/ws, nativesockets, chronos,chronicles, stew/byteutils
|
2021-03-11 03:34:14 +00:00
|
|
|
|
2021-03-18 15:30:21 +00:00
|
|
|
proc main() {.async.} =
|
2021-04-05 21:01:10 +00:00
|
|
|
let ws = await WebSocket.connect(
|
|
|
|
"127.0.0.1",
|
|
|
|
Port(8888),
|
2021-03-18 15:30:21 +00:00
|
|
|
path = "/ws")
|
2021-03-11 03:34:14 +00:00
|
|
|
|
2021-03-18 15:30:21 +00:00
|
|
|
debug "Websocket client: ", State = ws.readyState
|
|
|
|
|
|
|
|
let reqData = "Hello Server"
|
|
|
|
while true:
|
|
|
|
try:
|
|
|
|
await ws.send(reqData)
|
|
|
|
let buff = await ws.recv()
|
|
|
|
if buff.len <= 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
let dataStr = string.fromBytes(buff)
|
2021-04-05 21:01:10 +00:00
|
|
|
debug "Server Response: ", data = dataStr
|
2021-03-18 15:30:21 +00:00
|
|
|
|
|
|
|
assert dataStr == reqData
|
2021-04-05 21:01:10 +00:00
|
|
|
break
|
2021-03-18 15:30:21 +00:00
|
|
|
except WebSocketError as exc:
|
|
|
|
error "WebSocket error:", exception = exc.msg
|
|
|
|
|
|
|
|
await sleepAsync(100.millis)
|
|
|
|
|
|
|
|
# close the websocket
|
|
|
|
await ws.close()
|
|
|
|
|
|
|
|
waitFor(main())
|