-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make masking work like the spec. * Add jester sender * Add HTTP Beast support. * Fix tests. * v0.4.0
- Loading branch information
Showing
7 changed files
with
112 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,38 @@ | ||
import jester, ws, asyncdispatch, asynchttpserver, strutils, std/sha1, base64, nativesockets | ||
import ws, asyncdispatch, asynchttpserver, strutils, std/sha1, base64, nativesockets | ||
import jester, jester/private/utils | ||
|
||
proc newWebSocket*(req: jester.Request): Future[WebSocket] {.async.} = | ||
when useHttpBeast: | ||
import httpbeast, options, asyncnet | ||
|
||
|
||
proc newWebSocket*(req: httpbeast.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() | ||
) | ||
|
||
|
||
proc newWebSocket*(req: jester.Request): Future[WebSocket] {.async.} = | ||
## Creates a new socket from a jester request. | ||
when defined(useHttpBeast): | ||
raise newException("Websockets dont supprot http beast.") | ||
else: | ||
let req: asynchttpserver.Request = cast[asynchttpserver.Request](req.getNativeReq()) | ||
return await ws.newWebSocket(req) | ||
return await newWebSocket(req.getNativeReq()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import ws, asyncdispatch, asynchttpserver | ||
|
||
proc main() {.async.} = | ||
var ws = await newWebSocket("ws://0.0.0.0:5000/ws") | ||
echo await ws.receiveStrPacket() | ||
await ws.send("Hi, how are you?") | ||
echo await ws.receiveStrPacket() | ||
ws.close() | ||
|
||
waitFor main() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters