Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README.md #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,29 @@ Example chat server, will send what you send to connected all clients.
```nim
import ws, asyncdispatch, asynchttpserver

var connections = newSeq[WebSocket]()
var connections {.threadvar.}: seq[WebSocket]

proc initThread() =
connections = newSeq[WebSocket]()

proc cb(req: Request) {.async, gcsafe.} =
if req.url.path == "/ws":
try:
var ws = await newWebSocket(req)
connections.add ws
await ws.send("Welcome to simple chat server")
await ws.send("Welcome")
while ws.readyState == Open:
let packet = await ws.receiveStrPacket()
for other in connections:
if other.readyState == Open:
asyncCheck other.send(packet)
asyncCheck other.send(packet & " from the server")
except WebSocketError:
echo "socket closed:", getCurrentExceptionMsg()
else:
await req.respond(Http404, "Not found")


initThread()
var server = newAsyncHttpServer()
waitFor server.serve(Port(9001), cb)
```
Expand Down
Loading