https://github.com/ringabout/websocketx
Nim websocket for httpx.
https://github.com/ringabout/websocketx
Last synced: 4 months ago
JSON representation
Nim websocket for httpx.
- Host: GitHub
- URL: https://github.com/ringabout/websocketx
- Owner: ringabout
- License: mit
- Created: 2020-09-10T07:05:32.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-10-08T15:29:15.000Z (over 4 years ago)
- Last Synced: 2024-08-15T22:10:36.044Z (11 months ago)
- Language: Nim
- Size: 5.86 KB
- Stars: 13
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# websocketx
Nim websocket for httpx.Based on https://github.com/treeform/ws
## Installation
```
nimble install websocketx
```## Usage
### httpx
```nim
import options, asyncdispatch, httpx, websocketxproc onRequest(req: Request) {.async.} =
if req.path.isSome:
if req.path.get == "/ws":
var ws = await newWebSocket(req)
await ws.send("Welcome to simple echo server")
while ws.readyState == Open:
let packet = await ws.receiveStrPacket()
await ws.send(packet)
else:
req.send(Http404)
else:
req.send(Http404)run(onRequest)
```### asyncdispatch
```nim
import websocketx, asyncdispatch, asynchttpservervar 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.receiveStrPacket()
await ws.send(packet)
else:
await req.respond(Http404, "Not found")waitFor server.serve(Port(9001), cb)
```