mirror of
https://github.com/logos-storage/nim-websock.git
synced 2026-01-08 08:33:10 +00:00
* Use seq[byte] to store data. * Working bytes conversion. * Refactor the code. * Add test. * Add websocket test and fix closing handshake. * Add MsgReader to read data in external buffer. * rework frame reading * don't do toTitleCase * fix examples * use byte for more comfort * rework message reading + api * fix tests * adding specific exception types * minor cleanup * fixing tests * more tests * check the fin flag at the correct place * info for debug * split data not encoded frames * more tests * wip - control messages * closing flow and more explicit exception handling * test close and pings * add tests task to nimble * adding ci * change recv semantics * add frame tests * remove echo * better frame tests * fix * fix * handle continuation frames properly * more close logic handling * wip tests * handle close reasons properly * test control frames encoding * don't pass ws to event callbacks * fix masking and use correct base64 encoding * fix ci * addressing review comments * fix client example * i386 ci fix * wip ci * fix reading offset * don't read if socket closed * fix ci * wip * don't read if socket is closed Co-authored-by: Arijit Das <arijit@status.im> Co-authored-by: Arijit Das <arijitad.in@gmail.com>
38 lines
1.2 KiB
Nim
38 lines
1.2 KiB
Nim
import ../src/ws, ../src/http, chronos, chronicles, httputils, stew/byteutils
|
|
|
|
proc cb(transp: StreamTransport, header: HttpRequestHeader) {.async.} =
|
|
debug "Handling request:", uri = header.uri()
|
|
if header.uri() == "/ws":
|
|
debug "Initiating web socket connection."
|
|
try:
|
|
var ws = await createServer(header, transp, "")
|
|
if ws.readyState != Open:
|
|
error "Failed to open websocket connection."
|
|
return
|
|
|
|
debug "Websocket handshake completed."
|
|
while ws.readyState != ReadyState.Closed:
|
|
# Only reads header for data frame.
|
|
var recvData = await ws.recv()
|
|
if recvData.len <= 0:
|
|
debug "Empty messages"
|
|
break
|
|
|
|
# debug "Response: ", data = string.fromBytes(recvData), size = recvData.len
|
|
debug "Response: ", size = recvData.len
|
|
await ws.send(recvData)
|
|
# await ws.close()
|
|
|
|
except WebSocketError as exc:
|
|
error "WebSocket error:", exception = exc.msg
|
|
|
|
discard await transp.sendHTTPResponse(HttpVersion11, Http200, "Hello World")
|
|
await transp.closeWait()
|
|
|
|
when isMainModule:
|
|
let address = "127.0.0.1:8888"
|
|
var httpServer = newHttpServer(address, cb)
|
|
httpServer.start()
|
|
echo "Server started..."
|
|
waitFor httpServer.join()
|