Last active
August 29, 2015 14:24
-
-
Save dom96/721f126bc75c2979ca50 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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