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

  1. var express = require('express');
  2. var http = require('http');
  3. var io = require('socket.io');
  4. var bodyParser = require('body-parser');
  5. var cookieParser = require('cookie-parser');
  6. var app = express();
  7. app.use(express.static('./public'));
  8. //Specifying the public folder of the server to make the html accesible using the static middleware
  9. app.use(cookieParser());
  10. var jsonParser = bodyParser.json()
  11. // create application/x-www-form-urlencoded parser
  12. var urlencodedParser = bodyParser.urlencoded({ extended: false })
  13. // POST /login gets urlencoded bodies
  14. function findSocketByCookie(cookie) {
  15. for(var i in io.sockets.connected) {
  16. var socket = io.sockets.connected[i];
  17. if(socket.handshake.headers.cookie.indexOf(cookie) !== -1){
  18. return socket;
  19. }
  20. }
  21. }
  22. app.post('/show', urlencodedParser, function (req, res) {
  23. res.sendStatus(200);
  24. console.log("Sending json to live interfaces...");
  25. // console.dir(io.sockets.connected);
  26. for(var i in io.sockets.connected) {
  27. var socket = io.sockets.connected[i];
  28. socket.emit("jsonHoneycomb", req.body.honeyComb);
  29. }
  30. // var socket = findSocketByCookie(req.cookies.myUniqueCookie);
  31. });
  32. var server = http.createServer(app).listen(8080);
  33. console.log("Listening on port 8080");
  34. console.log("Send jsons to /show please.");
  35. //Server listens on the port 8124
  36. var io = io.listen(server);
  37. /*initializing the websockets communication , server instance has to be sent as the argument */
  38. io.sockets.on('connection', function(socket){
  39. console.log("New Client Connected.");
  40. socket.on('disconnect', function () {
  41. console.log("Client disconnected.")
  42. });
  43. socket.on('ackMessage', function(msg){
  44. if(msg == "true"){
  45. console.log('Message received.');
  46. }
  47. });
  48. socket.on('liveMessage', function(msg){
  49. if(msg == "true"){
  50. console.log('Live interface connected.');
  51. }
  52. });
  53. socket.on('jsonToParse', function(msg){
  54. if(msg == "true"){
  55. console.log('Json received.');
  56. socket.emit("jsonHoneycomb", msg);
  57. }
  58. });
  59. });