No description
Find a file
Jens Alfke 0ec63567ef
Don't sleep 1ms after every send (#25)
The loop in doSend is intended to pause 1ms in between 1MB chunks of
a very large frame. But it pauses even on small frames. This
introduces a 1ms delay after every send, effectively throttling
connections to no more than 1000 messages/second.

I simply changed the logic to sleep 1ms before every chunk *after the
first*. This change literally doubled the throughput of a client I'm
working on.

Fixes #24
2023-04-01 11:40:17 +03:00
src Don't sleep 1ms after every send (#25) 2023-04-01 11:40:17 +03:00
tests bump version, fix #15 2020-06-23 00:53:26 +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
news.nimble bump version, fix #15 2020-06-23 00:53:26 +03:00
README.md Fix assorted bugs and return packet kind (#8) 2019-11-18 14:32:12 +02:00

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")

waitFor server.serve(Port(9001), cb)

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.receiveString()
        echo "received ", packet

waitFor sendMsg()

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.receiveString()
        echo "received ", packet

waitFor sendMsg()

SSL/TLS connection is configured with a different prefix:

Note: not supported for chronos variant

var ws = await newWebSocket("wss://localhost/") # SSL context will be defaulted unless explicitly passed