Created
August 16, 2017 15:38
-
-
Save balzss/983d897d8458b5347130ea683ec0e1e6 to your computer and use it in GitHub Desktop.
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 express = require('express'); | |
var app = express(); | |
var server = require('http').createServer(app); | |
var socketioClient = require('socket.io')(server); | |
var mqtt = require('mqtt'); | |
app.use(express.static('public')); | |
var connectOptions = { | |
host: 'm2m.eclipse.org', | |
port: 1883 | |
}; | |
var mqttClient = mqtt.connect(connectOptions); | |
// szerven indításánál fut, amikor először csatlakozik az mqtt brókerhez | |
mqttClient.on('connect', function () { | |
// ezért itt kell feliratkozni a topikokra | |
mqttClient.subscribe('mortoff/oee2/restart'); | |
console.log('Connected to ' + connectOptions.host); | |
}); | |
// ez akkor fut amikor mqtt üzenet érkezik egy feliratkozott topikra | |
mqttClient.on('message', function (topic, message) { | |
console.log(message.toString()); | |
}); | |
// szerven indításánál fut, amikor először csatlakozik a socket.io klienshez | |
socketioClient.on('connection', function (socket) { | |
// it kell meghatározni, hogy beérkező socketio üzenetnél mi a teendő | |
socket.on('data', function (msg) { | |
console.log('socket msg in data:' + msg); | |
}); | |
}); | |
app.get('/', function (req, res) { | |
res.sendFile(__dirname + '/index.html'); | |
}); | |
server.listen(3000); |
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
npm install socket.io --save | |
npm install express --save | |
npm install mqtt --save |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment