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.

114 lines
3.0 KiB

  1. var express = require('express'),
  2. http = require('http'),
  3. io = require('socket.io'),
  4. bodyParser = require('body-parser'),
  5. cookieParser = require('cookie-parser'),
  6. mongojs = require('mongojs');
  7. var app = express();
  8. app.use(express.static('./public'));
  9. var mongoUri = "mongodb://localhost:27017/visualizer";
  10. var db = mongojs( mongoUri );
  11. var honeyCombs = db.collection('honeyCombs');
  12. var leaderBoards = db.collection('leaderBoards');
  13. // honeyCombs.insert({dio: "cane", type: "flyingPig"});
  14. //Specifying the public folder of the server to make the html accesible using the static middleware
  15. app.use(cookieParser());
  16. var jsonParser = bodyParser.json()
  17. // create application/x-www-form-urlencoded parser
  18. var urlencodedParser = bodyParser.urlencoded({ extended: false })
  19. // POST /login gets urlencoded bodies
  20. function findSocketByCookie(cookie) {
  21. for(var i in io.sockets.connected) {
  22. var socket = io.sockets.connected[i];
  23. if(socket.handshake.headers.cookie.indexOf(cookie) !== -1){
  24. return socket;
  25. }
  26. }
  27. }
  28. app.post('/show', urlencodedParser, function (req, res) {
  29. res.sendStatus(200);
  30. console.log("Sending json to live interfaces...");
  31. // console.dir(io.sockets.connected);
  32. for(var i in io.sockets.connected) {
  33. var socket = io.sockets.connected[i];
  34. socket.emit("jsonHoneycomb", req.body.honeyComb);
  35. saveQuery(req.body.honeyComb);
  36. }
  37. // var socket = findSocketByCookie(req.cookies.myUniqueCookie);
  38. });
  39. var server = http.createServer(app).listen(8080);
  40. console.log("Listening on port 8080");
  41. console.log("Send jsons to /show please.");
  42. //Server listens on the port 8124
  43. var io = io.listen(server);
  44. /*initializing the websockets communication , server instance has to be sent as the argument */
  45. io.sockets.on('connection', function(socket){
  46. console.log("New Client Connected.");
  47. socket.on('disconnect', function () {
  48. console.log("Client disconnected.")
  49. });
  50. socket.on('ackMessage', function(msg){
  51. if(msg == "true"){
  52. console.log('Message received.');
  53. }
  54. });
  55. socket.on('liveMessage', function(msg){
  56. if(msg == "true"){
  57. console.log('Live interface connected.');
  58. }
  59. });
  60. socket.on('jsonToParse', function(msg){
  61. console.log('Json received.');
  62. saveQuery(msg);
  63. // socket.emit("jsonHoneycomb", msg);
  64. });
  65. socket.on('leaderBoard',function(lb){
  66. leaderBoards.find().sort({timeStamp:-1}).limit(1, function(err, docs){
  67. var parsedLb = JSON.parse(lb);
  68. if( typeof docs[0].timeStamp != "undefined" ){
  69. if( parsedLb.timeStamp - docs[0].timeStamp > 600000){
  70. leaderBoards.insert(parsedLb);
  71. console.log("Saving stats report.");
  72. }
  73. }
  74. });
  75. //
  76. });
  77. });
  78. function saveQuery(query){
  79. var d = new Date();
  80. d =
  81. ("00" + (d.getMonth() + 1)).slice(-2) + "/" +
  82. ("00" + d.getDate()).slice(-2) + "/" +
  83. d.getFullYear() + " " +
  84. ("00" + d.getHours()).slice(-2) + ":" +
  85. ("00" + d.getMinutes()).slice(-2) + ":" +
  86. ("00" + d.getSeconds()).slice(-2)
  87. ;
  88. console.log("Saving query in db...");
  89. honeyCombs.insert({ honeyComb: query, dateStamp: d });
  90. }