mirror of
https://github.com/codex-storage/nim-websock.git
synced 2025-02-02 13:53:21 +00:00
6b76bd8261
* Update http to use chronos http. * Implement TLS in websocket. * Add webscoket TLS test. * Minor nit. * Add TLS test file. * Update http to use chronos http. (#6) * Update http to use chronos http. * Add stream.nim file. * Address comments. * Fix CI failure. * Minor change. * Address comments. * Fix windows CI failing test. * minor cleanup * spacess * more idiomatic connect * use stew/base10 Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com> * Implement TLS in websocket. * Minor nit. * merge master * wip * Update http to use chronos http. (#6) * Update http to use chronos http. * Add stream.nim file. * Address comments. * Fix CI failure. * Minor change. * Address comments. * Fix windows CI failing test. * minor cleanup * spacess * more idiomatic connect * use stew/base10 Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com> * Update http to use chronos http. * Implement TLS in websocket. * Minor nit. * Update http to use chronos http. (#6) * Update http to use chronos http. * Add stream.nim file. * Address comments. * Fix CI failure. * Minor change. * Address comments. * Fix windows CI failing test. * minor cleanup * spacess * more idiomatic connect * use stew/base10 Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com> * Implement TLS in websocket. * Minor nit. * add testing keys * wip * fix test * wip * remove eth dep and add skipdirs * fix package structure * fix deps * check nim version * Fix CI failure. * Don't call `ws.stream.closeWait()` * always close both ends to complete the sequence * misc * don't fail on close * Fix windows CI. * fix linux x86 builds * use consistent connect pattern * move keys to better place * return dumbResponse * small cleanup Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
50 lines
1.5 KiB
Nim
50 lines
1.5 KiB
Nim
import pkg/[chronos,
|
|
chronos/apps/http/httpserver,
|
|
chronicles,
|
|
httputils,
|
|
stew/byteutils]
|
|
|
|
import ../ws/ws
|
|
|
|
proc process(r: RequestFence): Future[HttpResponseRef] {.async.} =
|
|
if r.isOk():
|
|
let request = r.get()
|
|
debug "Handling request:", uri = request.uri.path
|
|
if request.uri.path == "/ws":
|
|
debug "Initiating web socket connection."
|
|
try:
|
|
var ws = await createServer(request,"")
|
|
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 "Client Response: ", data = string.fromBytes(recvData), size = recvData.len
|
|
debug "Client Response: ", size = recvData.len
|
|
await ws.send(recvData)
|
|
# await ws.close()
|
|
|
|
except WebSocketError as exc:
|
|
error "WebSocket error:", exception = exc.msg
|
|
discard await request.respond(Http200, "Hello World")
|
|
else:
|
|
return dumbResponse()
|
|
|
|
when isMainModule:
|
|
let address = initTAddress("127.0.0.1:8888")
|
|
let socketFlags = {ServerFlags.TcpNoDelay, ServerFlags.ReuseAddr}
|
|
let res = HttpServerRef.new(
|
|
address, process,
|
|
socketFlags = socketFlags)
|
|
|
|
let server = res.get()
|
|
server.start()
|
|
info "Server listening at ", data = address
|
|
waitFor server.join()
|