Advertisement
daixso

app.js

May 15th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express');
  2. var app = express();
  3. var serv = require('http').Server(app);
  4.  
  5.  
  6. //if request is empty send the index page
  7. app.get('/', function(req, res) {
  8.   res.sendFile(__dirname + '/client/index.html');
  9. });
  10.  
  11. //if request is for client send them to the client directory
  12. app.use('/client', express.static(__dirname + '/client'));
  13. serv.listen(2000);
  14. console.log('Server started');
  15.  
  16. var SOCKET_LIST = {};
  17.  
  18. var io = require('socket.io')(serv,{});
  19. io.sockets.on('connection', function(socket) {
  20.   socket.id = Math.random();
  21.   socket.x = 0;
  22.   socket.y = 0;
  23.  
  24.   SOCKET_LIST[socket.id] = socket;
  25. });
  26.  
  27. setInterval(function() {
  28.   for(var i in SOCKET_LIST) {
  29.     var socket = SOCKET_LIST[i];
  30.     socket.x++;
  31.     socket.y++;
  32.     socket.emit('newPosition',{
  33.       x:socket.x,
  34.       y:socket.y
  35.     });
  36.   }
  37. },1000/25);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement