mirror of
https://github.com/codex-storage/nim-websock.git
synced 2025-01-23 08:59:28 +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>
58 lines
1.9 KiB
Nim
58 lines
1.9 KiB
Nim
import pkg/[chronos,
|
|
chronos/apps/http/shttpserver,
|
|
chronicles,
|
|
httputils,
|
|
stew/byteutils]
|
|
|
|
import ../ws/ws
|
|
import ../tests/keys
|
|
|
|
let secureKey = TLSPrivateKey.init(SecureKey)
|
|
let secureCert = TLSCertificate.init(SecureCert)
|
|
|
|
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 == "/wss":
|
|
debug "Initiating web socket connection."
|
|
try:
|
|
var ws = await createServer(request, "myfancyprotocol")
|
|
if ws.readyState != Open:
|
|
error "Failed to open websocket connection."
|
|
return
|
|
debug "Websocket handshake completed."
|
|
# Only reads header for data frame.
|
|
echo "receiving server "
|
|
let recvData = await ws.recv()
|
|
if recvData.len <= 0:
|
|
debug "Empty messages"
|
|
break
|
|
|
|
if ws.readyState == ReadyState.Closed:
|
|
return
|
|
debug "Response: ", data = string.fromBytes(recvData)
|
|
await ws.send(recvData)
|
|
except WebSocketError:
|
|
error "WebSocket error:", exception = getCurrentExceptionMsg()
|
|
discard await request.respond(Http200, "Hello World")
|
|
else:
|
|
return dumbResponse()
|
|
|
|
when isMainModule:
|
|
let address = initTAddress("127.0.0.1:8888")
|
|
let serverFlags = {Secure, NotifyDisconnect}
|
|
let socketFlags = {ServerFlags.TcpNoDelay, ServerFlags.ReuseAddr}
|
|
let res = SecureHttpServerRef.new(
|
|
address, process,
|
|
serverFlags = serverFlags,
|
|
socketFlags = socketFlags,
|
|
tlsPrivateKey = secureKey,
|
|
tlsCertificate = secureCert)
|
|
|
|
let server = res.get()
|
|
server.start()
|
|
info "Server listening at ", data = address
|
|
waitFor server.join()
|