Skip to content

Instantly share code, notes, and snippets.

@battlecow
Created January 27, 2016 23:42
Show Gist options
  • Save battlecow/75835bb2085acacfbcf4 to your computer and use it in GitHub Desktop.
Save battlecow/75835bb2085acacfbcf4 to your computer and use it in GitHub Desktop.
<?php
function sendTestRequest($id) {
$socket = new \ZMQSocket(new \ZMQContext(), \ZMQ::SOCKET_REQ);
$socket->connect("tcp://192.168.98.142:5668");
$socket->send(json_encode(array("event" => 'test', "data" => $id)), \ZMQ::MODE_DONTWAIT);
$response = $socket->recv();
$parsedResponse = json_decode($response);
return $parsedResponse;
}
for ($i = 0; $i <= 10; $i++) {
$response = sendTestRequest($i);
echo "$i ---> $response\n";
}
var zmq = require('zmq');
var zmqResponder = zmq.socket('rep');
zmqResponder.on('message', function (msg, data) {
var parsed = JSON.parse(msg);
console.info('ZMQ Request received: ' + parsed.event);
setTimeout(function () {
console.log('received: ' + parsed.data);
zmqResponder.send(JSON.stringify('Message ID: ' + parsed.data));
}, (Math.floor(Math.random() * (10 - 5 + 1)) + 5) * 1000);
});
zmqResponder.bind('tcp://*:5668', function (err) {
if (err) {
console.error(err);
} else {
console.info("ZMQ awaiting orders on port 5668");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment