nim-websock/examples/client.nim
Dmitriy Ryajov 3e1599d790
Fix partial frame handling and allow extensions to hijack the flow (#56)
* moving files around

* wip

* wip

* move tls example into server example

* add tls functionality

* rename

* rename

* fix tests

* move extension related files to own folder

* use trace instead of debug

* export extensions

* rework partial frame handling and closing

* rework status codes as distincts

* logging

* re-enable extensions processing for frames

* enable all test for non-tls server

* remove tlsserver

* remove offset to mask - don't think we need it

* pass sessions extensions when calling send/recv

* adding encode/decode extensions flow test

* move server/client setup to helpers

* proper frame order execution on decode

* fix tls tests
2021-06-11 14:04:09 -06:00

46 lines
936 B
Nim

import pkg/[
chronos,
chronicles,
stew/byteutils]
import ../ws/ws
proc main() {.async.} =
let ws = when defined tls:
await WebSocket.connect(
"127.0.0.1",
Port(8888),
path = "/wss",
flags = {TLSFlags.NoVerifyHost, TLSFlags.NoVerifyServerName})
else:
await WebSocket.connect(
"127.0.0.1",
Port(8888),
path = "/ws")
trace "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)
trace "Server Response: ", data = dataStr
assert 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())