mirror of https://github.com/status-im/news.git
add gitignore, tests, readme:
This commit is contained in:
parent
35d14b09a4
commit
d55596b696
|
@ -0,0 +1,2 @@
|
|||
test
|
||||
*.exe
|
64
README.md
64
README.md
|
@ -1 +1,65 @@
|
|||
|
||||
# NEWS - Nim Easy WebSocket.
|
||||
|
||||
* Based on https://github.com/treeform/ws
|
||||
* Support `asyncdispatch` and https://github.com/status-im/nim-chronos
|
||||
|
||||
## Example Echo Server:
|
||||
|
||||
Example echo server, will repeat what you send it:
|
||||
|
||||
```nim
|
||||
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
|
||||
```nim
|
||||
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:
|
||||
```nim
|
||||
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:
|
||||
```nim
|
||||
var ws = await newWebsocket("wss://echo.websocket.org")
|
||||
```
|
|
@ -0,0 +1,23 @@
|
|||
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)
|
||||
|
||||
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()
|
Loading…
Reference in New Issue