Skip to content

Instantly share code, notes, and snippets.

@dom96
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dom96/721f126bc75c2979ca50 to your computer and use it in GitHub Desktop.
Save dom96/721f126bc75c2979ca50 to your computer and use it in GitHub Desktop.
import asyncnet, asyncdispatch, strtabs, parseutils, sha1, jester, htmlgen
type
WebSocketCallback = proc (client: WebSocket, message: WebSocketMessage) {.closure, gcsafe.}
WebSocketRecvClosure = proc (ws: WebSocket): Future[string] {.gcsafe, closure.}
WebSocketMessage = ref object
msg: string
WebSocket = ref object
socket: AsyncSocket
header: StringTableRef
onOpen: WebSocketCallback
onMessage: WebSocketCallback
onClose: WebSocketCallback
proc recv(ws: WebSocket, p: WebSocketRecvClosure): Future[string] {.async.}=
if not ws.socket.isClosed():
result = await ws.p()
if result == "":
ws.socket.close()
if ws.onClose != nil:
ws.onClose(ws, nil)
return result
proc reсvSize(ws: WebSocket, size: int): Future[string] {.async.} =
proc recvSizeClosure(ws: WebSocket): Future[string] {.async.} =
return await ws.socket.recv(size)
return await ws.recv(recvSizeClosure)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment