You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.9 KiB

var express = require('express');
var http = require('http');
var io = require('socket.io');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var app = express();
app.use(express.static('./public'));
//Specifying the public folder of the server to make the html accesible using the static middleware
app.use(cookieParser());
var jsonParser = bodyParser.json()
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
// POST /login gets urlencoded bodies
function findSocketByCookie(cookie) {
for(var i in io.sockets.connected) {
var socket = io.sockets.connected[i];
if(socket.handshake.headers.cookie.indexOf(cookie) !== -1){
return socket;
}
}
}
app.post('/show', urlencodedParser, function (req, res) {
res.sendStatus(200);
console.log("Sending json to live interfaces...");
// console.dir(io.sockets.connected);
for(var i in io.sockets.connected) {
var socket = io.sockets.connected[i];
socket.emit("jsonHoneycomb", req.body.honeyComb);
}
// var socket = findSocketByCookie(req.cookies.myUniqueCookie);
});
var server = http.createServer(app).listen(8080);
console.log("Listening on port 8080");
console.log("Send jsons to /show please.");
//Server listens on the port 8124
var io = io.listen(server);
/*initializing the websockets communication , server instance has to be sent as the argument */
io.sockets.on('connection', function(socket){
console.log("New Client Connected.");
socket.on('disconnect', function () {
console.log("Client disconnected.")
});
socket.on('ackMessage', function(msg){
if(msg == "true"){
console.log('Message received.');
}
});
socket.on('liveMessage', function(msg){
if(msg == "true"){
console.log('Live interface connected.');
}
});
socket.on('jsonToParse', function(msg){
if(msg == "true"){
console.log('Json received.');
socket.emit("jsonHoneycomb", msg);
}
});
});