news - nim easy web socket. Based on https://github.com/treeform/ws
Go to file
Volodymyr Melnychuk d55596b696 add gitignore, tests, readme: 2019-06-12 13:36:35 +03:00
src Chronos support (#1) 2019-06-11 17:09:54 +03:00
tests add gitignore, tests, readme: 2019-06-12 13:36:35 +03:00
.gitignore add gitignore, tests, readme: 2019-06-12 13:36:35 +03:00
LICENSE Initial commit 2019-05-08 16:06:46 +03:00
README.md add gitignore, tests, readme: 2019-06-12 13:36:35 +03:00
news.nimble initial commit 2019-05-08 16:10:48 +03:00

README.md

NEWS - Nim Easy WebSocket.

Example Echo Server:

Example echo server, will repeat what you send it:

import news, asyncdispatch, asynchttpserver

var server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
  if req.url.path == "/ws":
    var ws = await newWebsocket(req)
    await ws.send("Welcome to simple echo server")
    while ws.readyState == Open:
      let packet = await ws.receivePacket()
      await ws.send(packet)
  await req.respond(Http200, "Hello World")

asyncCheck server.serve(Port(9001), cb)
runForever()

Websocket client

Send messages to Echo server and receive unswer

import news, asyncdispatch

proc sendMsg() {.async.} =
    var ws = await newWebSocket("ws://localhost:9001/ws")
    await ws.send("hi")
    while ws.readyState == Open:
        let packet = await ws.receivePacket()
        echo "received ", packet

asyncCheck sendMsg()
runForever()

Websocket with chronos support:

import chronos

const newsUseChronos = true
include news

proc sendMsg() {.async.} =
    var ws = await newWebSocket("ws://localhost:9001/ws")
    await ws.send("hi")
    while ws.readyState == Open:
        let packet = await ws.receivePacket()
        echo "received ", packet

asyncCheck sendMsg()
runForever()

SSL is also supported:

var ws = await newWebsocket("wss://echo.websocket.org")