|
var Primus = require('primus'), |
|
http = require('http'), |
|
mqtt = require('mqtt'), |
|
util = require('util'), |
|
url = require('url'), |
|
fs = require('fs'), |
|
qs = require('querystring'), |
|
mq; |
|
|
|
// |
|
// Create http Server |
|
// |
|
var httpServer = http.createServer(function (req, res) { |
|
res.writeHead(200, { 'Content-Type': 'text/html' }); |
|
fs.createReadStream('index.html').pipe(res); |
|
}); |
|
|
|
// |
|
// Create WebSockets Server |
|
// |
|
var primus = new Primus(httpServer); |
|
|
|
primus.on('connection', function (spark) { |
|
spark.on('data', function (data) { |
|
var client = mq && mq.clients[data.client]; |
|
if (client) { |
|
client.publish({ topic: 'messages', payload: data.message }); |
|
} |
|
}); |
|
}); |
|
|
|
// |
|
// Create mqtt Server |
|
// |
|
var mqttServer = mqtt.createServer(function (client) { |
|
mq = this; |
|
|
|
if (!mq.clients) mq.clients = {}; |
|
|
|
client.on('connect', function (packet) { |
|
mq.clients[packet.clientId] = client; |
|
client.id = packet.clientId; |
|
client.subscriptions = []; |
|
console.log("Connected: " + client.id); |
|
client.connack({ returnCode: 0 }); |
|
}); |
|
|
|
client.on("publish", function (packet) { |
|
console.log(packet); |
|
}); |
|
|
|
client.on("subscribe", function (packet) { |
|
var granted = []; |
|
|
|
for (var i = 0; i < packet.subscriptions.length; i++) { |
|
var qos = packet.subscriptions[i].qos, |
|
topic = packet.subscriptions[i].topic; |
|
|
|
granted.push(qos); |
|
client.subscriptions.push(topic); |
|
} |
|
|
|
client.suback({ granted: granted, messageId: packet.messageId }); |
|
}); |
|
|
|
client.on('pingreq', function (packet) { |
|
client.pingresp(); |
|
}); |
|
|
|
client.on('disconnect', function (packet) { |
|
client.stream.end(); |
|
}); |
|
|
|
client.on('close', function (packer) { |
|
delete mq.clients[client.id]; |
|
}); |
|
|
|
client.on('error', function (err) { |
|
client.stream.end(); |
|
console.log(err); |
|
}); |
|
|
|
}); |
|
|
|
// |
|
// Start Servers |
|
// |
|
mqttServer.listen(1883); |
|
httpServer.listen(8080); |
|
console.log("MQTT and HTTP Server listening"); |
Hi, Nice to meet you.
My name is Edward, at WIZnet in Korea.
We have been searching some application references in which WIZnet solution is applied, and found your project “MQTT“ using Ethernet Shield. In the Ethernet Shield WZnet’s W5100 chip is embedded. Your development looks very cool & smart.
Recently we opened WIZnet Museum (http://wiznetmuseum.com) site. This is a academic-purposed collection of open projects, tutorials, articles and etc from our global customers.
If you are O.K. we would like to introduce your projects in here. Hopefully, you will allow this.
Also, if you are interested, we would like to send the Ethernet shield of our latest chip version, W5500 or WiFi Shield.
(This is free sample, and shipment fee is ours, but if some tax exists related with this sample, then you should pay import tax by yourself. Some countries ask tax!)
You may be able to establish another project with them.
Hopefully, keep contacting us for the friendship.
Thank you very much