src/nodejs/jswebsockets

  • WebSockets for JavaScript targets.

Example: cmd: -b:js -d:nodejs -r:off

import src/nodejs/jswebsockets
var socket = newWebSocket("ws://echo.websocket.org/")
socket.onopen = proc (e: Event) =
  echo("sent: test")
  socket.send("test")
socket.onmessage = proc (e: MessageEvent) =
  echo("received: ", e.data)
  socket.close(StatusCode(1000), "received msg")
socket.onclose = proc (e: CloseEvent) =
  echo("closing: ", e.reason)

Types

CloseEvent {.importc.} = object of Event
  code*: cint                ## https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Properties
  reason*: cstring
  wasClean*: bool
MessageEvent {.importc.} = object of Event
  data*, origin*: cstring
ReadyState {.pure.} = enum
  Connecting = 0, Open = 1, Closing = 2, Closed = 3
StatusCode = enum
  scNormal = 1000, scGoingAway = 1001, scProtocolError = 1002,
  scUnsupported = 1003, scReserved = 1004, scNoStatus = 1005, scAbnormal = 1006,
  scUnsupportedData = 1007, scPolicyViolation = 1008, scTooLarge = 1009,
  scMissingExt = 1010, scInternalError = 1011, scRestart = 1012,
  scTryAgainLater = 1013, scReserved2 = 1014, scTLSHandshake = 1015
WebSocket {.importc.} = ref object of EventTarget
  url*, protocol*: cstring
  readyState*: ReadyState
  onopen*: proc (event: Event)
  onclose*: proc (event: CloseEvent)
  onmessage*: proc (event: MessageEvent)

Procs

func addEventListener(et: EventTarget; ev: cstring;
                      cb: proc (ev: MessageEvent | CloseEvent)) {.importcpp,
    ...raises: [], tags: [].}
func close(socket: WebSocket) {.importcpp, ...raises: [], tags: [].}
func close(socket: WebSocket; code: StatusCode or Natural) {.importcpp,
    ...raises: [], tags: [].}
func close(socket: WebSocket; code: StatusCode or Natural; reason: cstring) {.
    importcpp, ...raises: [], tags: [].}
func newWebSocket(url: cstring): WebSocket {.importjs: "(new WebSocket(#))",
    ...raises: [], tags: [].}
func newWebSocket(url: cstring; protocol: cstring): WebSocket {.
    importjs: "(new WebSocket(#, @))", ...raises: [], tags: [].}
func newWebSocket(url: cstring; protocols: seq[cstring]): WebSocket {.
    importjs: "(new WebSocket(#, @))", ...raises: [], tags: [].}
func send(socket: WebSocket; data: cstring) {.importcpp, ...raises: [], tags: [].}