Created
February 6, 2012 12:04
-
-
Save adamvr/1751768 to your computer and use it in GitHub Desktop.
Submitted by observant - causes repeat transmissions?
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
| /** | |
| * Created by JetBrains WebStorm. | |
| * User: jima | |
| * Date: 6/02/12 | |
| * Time: 9:58 PM | |
| * To change this template use File | Settings | File Templates. | |
| */ | |
| var mqtt = require('mqttjs'); | |
| var argv = process.argv; | |
| for (var index = 2; index <= 4; index++) { | |
| if (!argv[index]) { | |
| process.exit(-1); | |
| } | |
| var port = argv[2]; | |
| var topic = argv[3]; | |
| var payload = argv[4]; | |
| var client = mqtt.createClient(port, "localhost", function(client) { | |
| client.connect({keepalive: 3}); | |
| client.on('connack', function(packet) { | |
| if (packet.returnCode === 0) { | |
| client.publish({topic: topic, payload: payload}); | |
| client.disconnect(); | |
| } else { | |
| console.log("connack error %d", packet.returnCode); | |
| process.exit(-1); | |
| } | |
| }); | |
| client.on('close', function() { | |
| process.exit(0); | |
| }); | |
| client.on('error', function(error) { | |
| console.log("error %s", error); | |
| process.exit(-1); | |
| }) | |
| }); | |
| } |
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
| var mqtt = require('mqttjs'); | |
| var server = mqtt.createServer(function(client) { | |
| var clients = {}; | |
| client.on('connect', function(packet) { | |
| console.log("[connect] %s", JSON.stringify(packet)); | |
| client.connack({returnCode: 0}); | |
| clients[client.id] = client; | |
| }); | |
| client.on('publish', function(packet) { | |
| console.log("[publish] %s", JSON.stringify(packet)); | |
| for (var clientId in clients) { | |
| clients[clientId].publish({topic: packet.topic, payload: packet.payload}); | |
| } | |
| }); | |
| client.on('subscribe', function(packet) { | |
| console.log("[subscribe] %s", JSON.stringify(packet)); | |
| }); | |
| client.on('pingreq', function(packet) { | |
| console.log("[pingreq] %s", JSON.stringify(packet)); | |
| }); | |
| client.on('disconnect', function(packet) { | |
| console.log("[disconnect] %s", JSON.stringify(packet)); | |
| }); | |
| client.on('close', function(packet) { | |
| console.log("[close] %s", JSON.stringify(packet)); | |
| }); | |
| client.on('error', function(packet) { | |
| console.log("[error] %s", JSON.stringify(packet)); | |
| }); | |
| }); | |
| server.listen(9999); | |
| console.log("Listening on port 9999"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment