guildenstern/websocketclient

This module allows easy creation of lots of websocket clients for testing purposes.

The WebsocketClientele object manages the operation and holds the set of all created clients in the clients seq parameter. (Note that the client at index position zero is always void client with id zero).

The individual clients are represented by the WebsocketClient object, which is inheritable.

Types

WebsocketClient {.inheritable.} = ref object
  id*: int
  url*: Uri
  socket*: SocketHandle = -1'i32
  clientele*: WebsocketClientele

Procs

proc close(client: WebsocketClient; handshake = true) {....raises: [],
    tags: [RootEffect], forbids: [].}
if you want to bypass the websocket close handshake dance, set the handhake to false.
proc connect(client: WebsocketClient; key = "dGhlIHNhbXBsZSBub25jZQ=="): bool {.
    ...raises: [], tags: [RootEffect], forbids: [].}
Tries to connect the client to a server. According to the spec, the key should be something random.
proc findClient(clientele: WebsocketClientele; socket: SocketHandle): WebsocketClient {.
    ...raises: [], tags: [], forbids: [].}
If such socket is not found, returns the empty client (client with id zero)
proc isConnected(client: WebsocketClient): bool {....raises: [], tags: [],
    forbids: [].}
proc newWebsocketClient(clientele: WebsocketClientele; url: string;
                        receive: proc (client: WebsocketClient) {.nimcall.}): WebsocketClient {.
    ...raises: [], tags: [], forbids: [].}
proc newWebsocketClientele(close: OnCloseSocketCallback = nil;
                           loglevel = LogLevel.WARN; bufferlength = 1000;
                           bytemask = "\v\x16!,"): WebsocketClientele {.
    ...raises: [], tags: [], forbids: [].}
Makes sense to keep the bufferlength low, if you are running thousands of clients. According to the spec, the bytemask should be random.
proc send(client: WebsocketClient; message: string; timeoutsecs = 10): bool {.
    discardable, ...raises: [], tags: [RootEffect, TimeEffect], forbids: [].}
If sending fails, closes the client automatically
proc start(clientele: WebsocketClientele; threadpoolsize = 0): bool {.
    ...raises: [ResourceExhaustedError],
    tags: [ReadIOEffect, RootEffect, TimeEffect], forbids: [].}

Iterators

iterator connectedClients(clientele: WebsocketClientele): WebsocketClient {.
    ...raises: [], tags: [], forbids: [].}