Skip to content

Commit

Permalink
fix installing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Oct 5, 2024
1 parent 20402b9 commit 5fd9370
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
33 changes: 32 additions & 1 deletion src/happyx/ssr/core.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


import
std/asyncnet,
std/net,
std/nativesockets,
std/os,
Expand All @@ -29,7 +30,8 @@ import
std/parseutils,
std/monotimes,
std/sugar,
std/deques
std/deques,
ws

import ioselectors

Expand Down Expand Up @@ -800,3 +802,32 @@ when false:
# TODO: Figure out the best way to implement this. One way is to use async
# events to signal our `eventLoop`. Maybe it would be better not to support
# multiple servers running at the same time?



proc newWebSocket*(req: Request): Future[WebSocket] {.async.} =
## Creates a new socket from an httpbeast request.
try:
let headers = req.headers.get

if not headers.hasKey("Sec-WebSocket-Version"):
req.send(Http404, "Not Found")
raise newException(WebSocketError, "Not a valid websocket handshake.")

var ws = WebSocket()
ws.masked = false

# Here is the magic:
req.forget() # Remove from HttpBeast event loop.
asyncdispatch.register(req.client.AsyncFD) # Add to async event loop.

ws.tcpSocket = newAsyncSocket(req.client.AsyncFD)
await ws.handshake(headers)
return ws

except ValueError, KeyError:
# Wrap all exceptions in a WebSocketError so its easy to catch
raise newException(
WebSocketError,
"Failed to create WebSocket from request: " & getCurrentExceptionMsg()
)
5 changes: 4 additions & 1 deletion src/happyx/ssr/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ else:
when enableHttpBeast:
import websocket
export websocket
else:
elif not enableBuiltin:
import websocketx
export websocketx
else:
import ws
export ws


when enableDefaultComponents:
Expand Down

0 comments on commit 5fd9370

Please sign in to comment.