Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 2.10 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Required dependancies
  2. var io = require('socket.io');
  3. var app = require('express').createServer();
  4. var fs = require('fs');
  5.  
  6. // State is the current slide position
  7. var state = 1
  8. // Clients is a list of users who have connected
  9. var clients = [];
  10. // Bind socket.io to express
  11. var socket = io.listen(app);
  12.  
  13. // For each connection made add the client to the
  14. // list of clients.
  15. socket.on('connection', function(client) {
  16.   clients.push(client);
  17. });
  18.  
  19. // This is a simple wrapper for sending a message
  20. // to all the connected users and pruning out the
  21. // disconnected ones.
  22. function send(message) {
  23.   // Iterate through all potential clients
  24.   clients.forEach(function(client) {
  25.     // User is still connected, send message
  26.     if(client._open) {
  27.       client.send(message);
  28.     }
  29.     // Prune out disconnected user
  30.     else {
  31.       delete client;
  32.     }
  33.   });
  34. }
  35.  
  36. // Advancing will... move the slides forward!
  37. app.get('/advance', function(req, res) {
  38.   // Increment and send over socket
  39.   state++;
  40.   send({ state: state });
  41.  
  42.   // Send the state as a response
  43.   res.send(state.toString());
  44. });
  45.  
  46. // Receding will... move the slides backwards!
  47. app.get('/recede', function(req, res) {
  48.   state--;
  49.   send({ state: state });
  50.  
  51.   res.send(state.toString());
  52. });
  53.  
  54. // This will allow the presenter to clear the
  55. // slides of any cornification.
  56. app.get('/refresh', function(req, res) {
  57.   client.send({ refresh: true });
  58.  
  59.   res.send(state.toString());
  60. });
  61.  
  62. // Reset will not refresh cornfication, but
  63. // will send the slides back to the beginning.
  64. app.get('/reset', function(req, res) {
  65.   state = 1;
  66.   send({ state: state });
  67.  
  68.   res.send(state.toString());
  69. });
  70.  
  71. // Give your viewers what they really want...
  72. // an unrepentable amount of unicorns.
  73. app.get('/cornify', function(req, res) {
  74.   send({ cornify: true });
  75.  
  76.   res.send(state.toString());
  77. });
  78.  
  79. // Send the controller for any other request to this
  80. // Node.js server.
  81. app.get('*', function(req, res) {
  82.   fs.readFile('controller.html', function(err, buffer) {
  83.     res.send(buffer.toString());
  84.   });
  85. });
  86.  
  87. // Listen on some high level port to avoid dealing
  88. // with authbind or root user privileges.
  89. app.listen(1987);