nim-websock/examples/client.nim

54 lines
1.2 KiB
Nim
Raw Permalink Normal View History

2021-06-27 06:35:36 +00:00
## nim-websock
## 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.
WIP: Implement websocket TLS. (#7) * Update http to use chronos http. * Implement TLS in websocket. * Add webscoket TLS test. * Minor nit. * Add TLS test file. * Update http to use chronos http. (#6) * Update http to use chronos http. * Add stream.nim file. * Address comments. * Fix CI failure. * Minor change. * Address comments. * Fix windows CI failing test. * minor cleanup * spacess * more idiomatic connect * use stew/base10 Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com> * Implement TLS in websocket. * Minor nit. * merge master * wip * Update http to use chronos http. (#6) * Update http to use chronos http. * Add stream.nim file. * Address comments. * Fix CI failure. * Minor change. * Address comments. * Fix windows CI failing test. * minor cleanup * spacess * more idiomatic connect * use stew/base10 Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com> * Update http to use chronos http. * Implement TLS in websocket. * Minor nit. * Update http to use chronos http. (#6) * Update http to use chronos http. * Add stream.nim file. * Address comments. * Fix CI failure. * Minor change. * Address comments. * Fix windows CI failing test. * minor cleanup * spacess * more idiomatic connect * use stew/base10 Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com> * Implement TLS in websocket. * Minor nit. * add testing keys * wip * fix test * wip * remove eth dep and add skipdirs * fix package structure * fix deps * check nim version * Fix CI failure. * Don't call `ws.stream.closeWait()` * always close both ends to complete the sequence * misc * don't fail on close * Fix windows CI. * fix linux x86 builds * use consistent connect pattern * move keys to better place * return dumbResponse * small cleanup Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
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
proc main() {.async.} =
let ws = when defined tls:
await WebSocket.connect(
"127.0.0.1:8889",
path = "/wss",
secure = true,
flags = {TLSFlags.NoVerifyHost, TLSFlags.NoVerifyServerName})
else:
await WebSocket.connect(
"127.0.0.1:8888",
path = "/ws")
trace "Websocket client: ", State = ws.readyState
let reqData = "Hello Server"
while true:
try:
await ws.send(reqData)
2021-05-22 09:04:40 +00:00
let buff = await ws.recv()
if buff.len <= 0:
break
let dataStr = string.fromBytes(buff)
trace "Server Response: ", data = dataStr
doAssert dataStr == reqData
break
except WebSocketError as exc:
error "WebSocket error:", exception = exc.msg
raise exc
await sleepAsync(100.millis)
# close the websocket
await ws.close()
waitFor(main())