src/ws

Source   Edit  

Types

Opcode = enum
  Cont = 0,                 ## Denotes a continuation frame.
  Text = 1,                 ## Denotes a text frame.
  Binary = 2,               ## Denotes a binary frame.
  Close = 8,                ## Denotes a connection close.
  Ping = 9,                 ## Denotes a ping.
  Pong = 10                  ## Denotes a pong.
4 bits. Defines the interpretation of the "Payload data". Source   Edit  
ReadyState = enum
  Connecting = 0, Open = 1, Closing = 2, Closed = 3
Source   Edit  
WebSocket = ref object
  tcpSocket*: AsyncSocket
  version*: int
  key*: string
  protocol*: string
  readyState*: ReadyState
  masked*: bool
Source   Edit  
WebSocketError = object of IOError
Source   Edit  

Procs

proc close(ws: WebSocket) {....raises: [Exception, SslError, ValueError,
                                     LibraryError], tags: [RootEffect,
    WriteIOEffect, ReadIOEffect, TimeEffect], forbids: [].}
Close the Socket, sends close packet. Source   Edit  
proc decodeBase16(str: string): string {....raises: [], tags: [], forbids: [].}
Base16 decode a string. Source   Edit  
proc encodeBase16(str: string): string {....raises: [], tags: [], forbids: [].}
Base61 encode a string. Source   Edit  
proc handshake(ws: WebSocket; headers: HttpHeaders): owned(Future[void]) {.
    ...stackTrace: false, raises: [Exception, ValueError, KeyError,
                                WebSocketProtocolMismatchError, SslError],
    tags: [RootEffect], forbids: [].}
Handles the websocket handshake. Source   Edit  
proc hangup(ws: WebSocket) {....raises: [WebSocketError], tags: [RootEffect],
                             forbids: [].}
Closes the Socket without sending a close packet. Source   Edit  
proc newWebSocket(req: Request; protocol: string = ""): Future[WebSocket] {.
    ...stackTrace: false, raises: [Exception, ValueError, WebSocketHandshakeError,
                                SslError, WebSocketProtocolMismatchError,
                                WebSocketCreationError], tags: [RootEffect],
    forbids: [].}
Creates a new socket from a request. Source   Edit  
proc newWebSocket(url: string; protocol: string): Future[WebSocket] {.
    ...stackTrace: false, raises: [Exception, ValueError, OSError, WebSocketError,
                                KeyError, HttpRequestError, LibraryError,
                                SslError, IOError, ProtocolError,
                                WebSocketFailedUpgradeError,
                                WebSocketProtocolMismatchError],
    tags: [RootEffect, ReadIOEffect, TimeEffect], forbids: [].}
Source   Edit  
proc newWebSocket(url: string; protocols: seq[string] = @[]): Future[WebSocket] {.
    ...stackTrace: false, raises: [Exception, ValueError, OSError, WebSocketError,
                                KeyError, HttpRequestError, LibraryError,
                                SslError, IOError, ProtocolError,
                                WebSocketFailedUpgradeError,
                                WebSocketProtocolMismatchError],
    tags: [RootEffect, ReadIOEffect, TimeEffect], forbids: [].}
Creates a new WebSocket connection, protocol is optional, "" means no protocol. Source   Edit  
proc ping(ws: WebSocket; data = ""): owned(Future[void]) {....stackTrace: false,
    raises: [Exception, SslError, ValueError],
    tags: [RootEffect, WriteIOEffect, ReadIOEffect, TimeEffect], forbids: [].}
Sends a ping to the other end, both server and client can send a ping. Data is optional. Source   Edit  
proc receiveBinaryPacket(ws: WebSocket): Future[seq[byte]] {....stackTrace: false, raises: [
    Exception, ValueError, WebSocketClosedError, WebSocketError, SslError,
    WebSocketPacketTypeError],
    tags: [RootEffect, WriteIOEffect, ReadIOEffect, TimeEffect], forbids: [].}
Wait only for a binary packet to come. Errors out on string packets. Source   Edit  
proc receivePacket(ws: WebSocket): Future[(Opcode, string)] {....stackTrace: false, raises: [
    Exception, ValueError, WebSocketClosedError, WebSocketError, SslError],
    tags: [RootEffect], forbids: [].}
Wait for a string or binary packet to come in. Source   Edit  
proc receiveStrPacket(ws: WebSocket): Future[string] {....stackTrace: false, raises: [
    Exception, ValueError, WebSocketClosedError, WebSocketError, SslError,
    WebSocketPacketTypeError],
    tags: [RootEffect, WriteIOEffect, ReadIOEffect, TimeEffect], forbids: [].}
Wait only for a string packet to come. Errors out on Binary packets. Source   Edit  
proc send(ws: WebSocket; text: string; opcode = Opcode.Text): Future[void] {.
    ...stackTrace: false, raises: [Exception, SslError],
    tags: [RootEffect, WriteIOEffect, ReadIOEffect, TimeEffect], forbids: [].}
This is the main method used to send data via this WebSocket. Source   Edit  
proc setupPings(ws: WebSocket; seconds: float) {.
    ...raises: [Exception, SslError, ValueError],
    tags: [RootEffect, WriteIOEffect, ReadIOEffect, TimeEffect], forbids: [].}
Sets a delay on when to send pings. Source   Edit  
proc toSeq(hv: HttpHeaderValues): seq[string] {....raises: [], tags: [],
    forbids: [].}
Casts HttpHeaderValues to seq of strings. Source   Edit