Skip to content

Instantly share code, notes, and snippets.

@tinku99
Created April 18, 2012 23:26
Show Gist options
  • Select an option

  • Save tinku99/2417355 to your computer and use it in GitHub Desktop.

Select an option

Save tinku99/2417355 to your computer and use it in GitHub Desktop.
send and receive ahk objects using json / zmq
z := zstart("tcp://localhost:5555")
omsg := ["what", "else"]
zsendrcv(z, omsg)
return
;; ztart()
zstart(endpoint="tcp://localhost:5555"){
z := new zmq()
z.context := z.zmq_init(1)
tooltip, % "Connecting to " endpoint
z.requester := z.zmq_socket(z.context, z.ZMQ_REQ)
hr := z.zmq_connect(z.requester, endpoint)
assert(hr)
return z
}
;; zsendrecvjson(z, omsg)
zsendrecvjson(z, omsg){
VarSetCapacity(request, 16, 0)
VarSetCapacity(reply, 16, 0)
if !omsg
omsg := ["hello", "world"]
msg := json_to(omsg)
hr := z.zmq_msg_init_size(&request, strlen(msg))
assert(hr)
msgp := z.zmq_msg_data(&request)
strput(msg, msgp, strlen(msg) , "utf-8")
tooltip % "Sending`n" msg
hr := z.zmq_send(z.requester, &request, 0)
assert(hr)
hr := z.zmq_msg_close(&request)
assert(hr)
hr := z.zmq_msg_init(&reply)
assert(hr)
hr := z.zmq_recv(z.requester, &reply, 0)
assert(hr)
size := z.zmq_msg_size(&reply)
msgp := z.zmq_msg_data(&reply)
msg := StrGet(msgp, size, "utf-8")
hr := z.zmq_msg_close(&reply)
; tooltip, % "Received " msg " of size: " size
jmsg := json_from(msg)
tooltip % tostring(jmsg)
return jmsg
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment