const zmq = require('zeromq'); const address = 'tcp://127.0.0.1:12345'; let router = zmq.socket('router'); router.bind(address, (err) => { if (err) { console.log(err); } else { console.log('Listening on 5563...'); } }); router.on('message', (...args) => { let msg = args.map((a) => a.toString()); let id = args[0]; setTimeout(() => { let n = Math.ceil(Math.random() * 50) - 10; router.send([id, "weather", n, msg[1]]); }, 100); });
const zmq = require('zeromq'); const address = 'tcp://127.0.0.1:12345'; var dealer = zmq.socket('dealer'); dealer.connect(address); dealer.identity = String(Math.random()).substring(2); dealer.on('message', (...args) => { let msg = args.map((a) => a.toString()); console.log('received', msg); }); setInterval(() => { let msg = ["weatherinfo" + dealer.identity]; console.log('send', msg); dealer.send(msg); }, 30);
const zmq = require('zeromq'); const address = 'tcp://127.0.0.1:12345'; const net = require('net'); const keyPair = zmq.curveKeyPair(); let router = new zmq.Router({ curveServer: false, curvePublicKey: keyPair.publicKey, curveSecretKey: keyPair.secretKey, curveServerKey: 'Z-s:wV#DnaWa*I%^s!(xg^4)GSf^o2h1&E95Se.^' }); const listen = async () => { for await (const [sender, msg] of router) { console.log("received: %s", Buffer.from(sender).toString('base64'), msg.toString()); setTimeout(() => { let n = Math.ceil(Math.random() * 50) - 10; router.send([sender, "hoi12345#"]); }, 100); } } const listenForEvents = async () => { for await (event of router.events) { console.log(`${event.type} ${JSON.stringify(event)}`) } } const run = async () => { await router.bind(address); await Promise.all([ listen(), listenForEvents() ]); }; run();
const zmq = require('zeromq'); const fs = require('fs'); const address = 'tcp://127.0.0.1:12345'; let i = 1; const run = async () => { const fileName = "server.json"; let keyPair = null; if (fs.existsSync(fileName)) { keyPair = JSON.parse(fs.readFileSync("server.json")); } else { keyPair = zmq.curveKeyPair(); fs.writeFileSync("server.json", JSON.stringify(keyPair, null, 4)); } var dealer = new zmq.Dealer({ curveServer: true, curvePublicKey: keyPair.publicKey, curveSecretKey: keyPair.secretKey, }); dealer.connect(address); setInterval(() => { let msg = [[ "weatherinfo", i ]]; // + dealer.identity i += 1; console.log('send', msg); dealer.send(msg); }, 300); console.log("receiving..."); for await (const [msg] of dealer) { console.log("received: %s", msg.toString()) } } run();
116200cookie-checkZeroMQ (zmq) Router-Dealer