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.

122 lines
3.1 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. // leaderBoards.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( docs.length ){
  69. if(typeof docs[0].timeStamp != "undefined"){
  70. if( parsedLb.timeStamp - docs[0].timeStamp > 600000){
  71. leaderBoards.insert(parsedLb);
  72. console.log("Saving stats report.");
  73. }
  74. }
  75. }else{
  76. leaderBoards.insert(parsedLb);
  77. }
  78. if(err){
  79. console.dir(err);
  80. }
  81. });
  82. //
  83. });
  84. });
  85. function saveQuery(query){
  86. var d = new Date();
  87. d =
  88. ("00" + (d.getMonth() + 1)).slice(-2) + "/" +
  89. ("00" + d.getDate()).slice(-2) + "/" +
  90. d.getFullYear() + " " +
  91. ("00" + d.getHours()).slice(-2) + ":" +
  92. ("00" + d.getMinutes()).slice(-2) + ":" +
  93. ("00" + d.getSeconds()).slice(-2)
  94. ;
  95. console.log("Saving query in db...");
  96. honeyCombs.insert({ honeyComb: query, dateStamp: d });
  97. }