Skip to content

Instantly share code, notes, and snippets.

@runvnc
Created March 5, 2014 06:44
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 runvnc/9362338 to your computer and use it in GitHub Desktop.
Save runvnc/9362338 to your computer and use it in GitHub Desktop.
when it was a ttable[string, tsocket] didnt seem to find my ids
import httpserver, asyncio, sockets, strutils, tables, oids, cgi, msgqredis, marshal, hashes
var myrespq = ""
type TQueuedRequest = object
responseQueue*:string
requestId*:string
path*: string
query*: string
type
TReqSockets = TTable[int, TSocket]
var reqSockets: TReqSockets = initTable[int, TSocket]()
proc uniqidstr():string =
var sid:string = $(genOid())
sid.delete(sid.len-2, sid.len-1)
return sid
proc uniqid():int =
return hash(uniqidstr())
proc newMessage(queue:string, msg:seq[string]) =
echo "got a new response message in asynchttp!"
echo("length of msg seq is " & $(msg.len))
echo("first: " & msg[0])
echo("second: " & msg[1])
var sock = reqSockets[ParseInt(msg[0])] #msg.requestId]
if sock != nil:
sock.send(msg[1])
sock.close()
else:
echo "Can't find socket with id " & msg[0] # & msg.requestId
echo "reqSockets is:"
for k, v in reqSockets.pairs:
echo("key is [" & $k & "] and val is " & $(v.getSockName()))
myrespq = uniqid()
msgqredis.init(newMessage, myrespq)
proc queueRequest(client:TSocket, path:string, query:string) =
# get unique id for this request
var reqid = uniqid()
echo("reqid is ----- " & $reqid)
var reqidstr: string = $reqid
var req = TQueuedRequest(responseQueue: myrespq, requestId: reqidstr,
path:path, query: query)
# push request data with unique id and list I am listening on
reqSockets[reqid] = client
echo("reqSockets[" & $reqid & "] sockname is " & $(client.getSockName()))
echo "queueing request"
var len = pushMessage("httpq", $$req)
proc handleRequest(server: PAsyncHTTPServer; client: TSocket;
path, query: string): bool =
try:
var query2 = URLdecode(query)
if path.startsWith("/static"):
serveFile(client, "." & path)
client.close()
else:
queueRequest(client, path, query2)
except:
echo("Exception in handler.")
finally:
try:
#client.close()
except:
echo("Error in close")
var disp = newDispatcher()
var http = asyncHTTPServer(handleRequest, TPort(3011))
disp.register(http)
while true:
if not disp.poll():
echo("HTTP server has been closed.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment