2021-06-27 06:35:36 +00:00
|
|
|
## nim-websock
|
2021-06-20 04:33:20 +00:00
|
|
|
## Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2021-04-13 22:05:58 +00:00
|
|
|
import pkg/[
|
|
|
|
chronos,
|
|
|
|
chronicles,
|
|
|
|
stew/byteutils]
|
|
|
|
|
2021-06-27 06:35:36 +00:00
|
|
|
import ../websock/websock
|
2021-03-11 03:34:14 +00:00
|
|
|
|
2021-03-18 15:30:21 +00:00
|
|
|
proc main() {.async.} =
|
2021-06-11 20:04:09 +00:00
|
|
|
let ws = when defined tls:
|
|
|
|
await WebSocket.connect(
|
2021-07-15 20:17:55 +00:00
|
|
|
"127.0.0.1:8889",
|
2021-06-11 20:04:09 +00:00
|
|
|
path = "/wss",
|
2021-07-15 20:17:55 +00:00
|
|
|
secure = true,
|
2021-06-11 20:04:09 +00:00
|
|
|
flags = {TLSFlags.NoVerifyHost, TLSFlags.NoVerifyServerName})
|
|
|
|
else:
|
|
|
|
await WebSocket.connect(
|
2021-07-15 00:51:39 +00:00
|
|
|
"127.0.0.1:8888",
|
2021-06-11 20:04:09 +00:00
|
|
|
path = "/ws")
|
|
|
|
|
|
|
|
trace "Websocket client: ", State = ws.readyState
|
2021-03-18 15:30:21 +00:00
|
|
|
|
|
|
|
let reqData = "Hello Server"
|
|
|
|
while true:
|
|
|
|
try:
|
|
|
|
await ws.send(reqData)
|
2021-05-22 09:04:40 +00:00
|
|
|
let buff = await ws.recv()
|
2021-03-18 15:30:21 +00:00
|
|
|
if buff.len <= 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
let dataStr = string.fromBytes(buff)
|
2021-06-11 20:04:09 +00:00
|
|
|
trace "Server Response: ", data = dataStr
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2021-07-15 20:17:55 +00:00
|
|
|
doAssert 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
|
2021-06-01 02:39:14 +00:00
|
|
|
raise exc
|
2021-03-18 15:30:21 +00:00
|
|
|
|
|
|
|
await sleepAsync(100.millis)
|
|
|
|
|
|
|
|
# close the websocket
|
|
|
|
await ws.close()
|
|
|
|
|
|
|
|
waitFor(main())
|