mirror of
https://github.com/logos-storage/nim-websock.git
synced 2026-01-09 17:13:09 +00:00
* wip * wip * move http under ws folder * use asyctest * wip * wip * rework response sending * make example work with latest changes * wip request/response * misc * fix example to use new http layer * pass tls flags to client * more cleanup * unused imports * more unsused imports * better headers * add helpre sendError * export sendError * attach selected proto to session * move proto to session * handle unsupported version * fix tests * comment out for now * fix utf8 tests * allow tests to be ran in tls * misc * use Port type * add tls flags * better api * run tls tests * fix tests on windows * allow running tests with tls * mic * wip * fix autobahn ci * handle close * cleanup * logging and error handling * remove old stream
54 lines
1.6 KiB
Nim
54 lines
1.6 KiB
Nim
import pkg/[chronos,
|
|
chronicles,
|
|
httputils,
|
|
stew/byteutils]
|
|
|
|
import pkg/[chronos/streams/tlsstream]
|
|
|
|
import ../ws/ws
|
|
import ../tests/keys
|
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
debug "Handling request:", uri = request.uri.path
|
|
if request.uri.path != "/wss":
|
|
debug "Initiating web socket connection."
|
|
return
|
|
|
|
try:
|
|
let server = WSServer.new(protos = ["myfancyprotocol"])
|
|
var ws = await server.handleRequest(request)
|
|
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,
|
|
if ws.binary: Opcode.Binary else: Opcode.Text)
|
|
except WebSocketError:
|
|
error "WebSocket error:", exception = getCurrentExceptionMsg()
|
|
|
|
when isMainModule:
|
|
proc main() {.async.} =
|
|
let address = initTAddress("127.0.0.1:8888")
|
|
let socketFlags = {ServerFlags.TcpNoDelay, ServerFlags.ReuseAddr}
|
|
let server = TlsHttpServer.create(
|
|
address = address,
|
|
handler = handle,
|
|
tlsPrivateKey = TLSPrivateKey.init(SecureKey),
|
|
tlsCertificate = TLSCertificate.init(SecureCert),
|
|
flags = socketFlags)
|
|
|
|
server.start()
|
|
info "Server listening at ", data = $server.localAddress()
|
|
await server.join()
|
|
|
|
waitFor(main()) |