Example
import websocket, asyncnet, asyncdispatch let ws = waitFor newAsyncWebsocketClient("localhost", Port(8080), path = "/", protocols = @["myfancyprotocol"]) echo "connected!" proc ping() {.async.} = while true: await sleepAsync(6000) echo "ping" await ws.sendPing() proc read() {.async.} = while true: let (opcode, data) = await ws.readData() echo "(opcode: ", opcode, ", data: ", data, ")" asyncCheck read() asyncCheck ping() runForever()
Consts
WebsocketUserAgent = "websocket.nim (https://github.com/niv/websocket.nim)"
- Source Edit
Procs
proc newAsyncWebsocketClient(uri: Uri; client: AsyncHttpClient; protocols: seq[string] = @[]): Future[AsyncWebSocket] {...}{. raises: [Exception, ValueError, FutureError], tags: [TimeEffect, ReadIOEffect, RootEffect].}
- Create a new websocket and connect immediately. Optionally give a list of protocols to negotiate; keep empty to accept the one the server offers (if any). The negotiated protocol is in AsyncWebSocket.protocol. Source Edit
proc newAsyncWebsocketClient(uri: Uri; additionalHeaders: seq[(string, string)] = @[]; protocols: seq[string] = @[]; userAgent: string = WebsocketUserAgent; sslContext: SslContext = getDefaultSsl()): Future[ AsyncWebSocket] {...}{.raises: [KeyError, Exception, ValueError, FutureError], tags: [TimeEffect, ReadIOEffect, RootEffect].}
- Source Edit
proc newAsyncWebsocketClient(uri: string; additionalHeaders: seq[(string, string)] = @[]; protocols: seq[string] = @[]; userAgent: string = WebsocketUserAgent; sslContext: SslContext = getDefaultSsl()): Future[ AsyncWebSocket] {...}{.raises: [KeyError, Exception, ValueError, FutureError], tags: [TimeEffect, ReadIOEffect, RootEffect].}
- Source Edit
proc newAsyncWebsocketClient(host: string; port: Port; path: string; ssl = false; additionalHeaders: seq[(string, string)] = @[]; protocols: seq[string] = @[]; userAgent: string = WebsocketUserAgent; sslContext: SslContext = getDefaultSsl()): Future[ AsyncWebSocket] {...}{.raises: [KeyError, Exception, ValueError, FutureError], tags: [TimeEffect, ReadIOEffect, RootEffect].}
- Source Edit