Example
let ws = waitFor newAsyncWebsocket("echo.websocket.org", Port 80, "/?encoding=text", ssl = false) echo "connected!" proc reader() {.async.} = while true: let read = await ws.sock.readData(true) echo "read: " & $read proc ping() {.async.} = while true: await sleepAsync(6000) echo "ping" await ws.sock.sendPing(true) asyncCheck reader() asyncCheck ping() runForever()
Procs
proc newAsyncWebsocket*(host: string; port: Port; path: string; ssl = false; additionalHeaders: seq[(string, string)] = @ []; protocols: seq[string] = @ []): Future[AsyncWebSocket] {.async.}
- 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
proc close*(ws: AsyncWebSocket): Future[void] {.async.}
- Closes the socket. Source