Advertisement
Guest User

routes.js

a guest
Nov 20th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This file is required by app.js. It sets up event listeners
  2. // for the two main URL endpoints of the application - /create and /chat/:id
  3. // and listens for socket.io messages.
  4.  
  5. // Use the gravatar module, to turn email addresses into avatar images:
  6.  
  7. var gravatar = require('gravatar');
  8.  
  9. // Export a function, so that we can pass
  10. // the app and io instances from the app.js file:
  11.  
  12. module.exports = function(app,io){
  13.  
  14.     app.get('/', function(req, res){
  15.  
  16.         // Render views/home.html
  17.         res.render('home');
  18.     });
  19.  
  20.     app.get('/create', function(req,res){
  21.  
  22.         // Generate unique id for the room
  23.         var id = Math.round((Math.random() * 1000000));
  24.  
  25.         // Redirect to the random room
  26.         res.redirect('/chat/'+id);
  27.     });
  28.  
  29.     app.get('/chat/:id', function(req,res){
  30.  
  31.         // Render the chant.html view
  32.         res.render('chat');
  33.     });
  34.  
  35.     // Initialize a new socket.io application, named 'chat'
  36.     var chat = io.on('connection', function (socket) {
  37.  
  38.         // When the client emits the 'load' event, reply with the
  39.         // number of people in this chat room
  40.  
  41.         socket.on('load',function(data){
  42.  
  43.             var room = findClientsSocket(io,data);
  44.             if(room.length === 0 ) {
  45.  
  46.                 socket.emit('peopleinchat', {number: 0});
  47.             }
  48.             else if(room.length === 1) {
  49.  
  50.                 socket.emit('peopleinchat', {
  51.                     number: 1,
  52.                     user: room[0].username,
  53.                     avatar: room[0].avatar,
  54.                     id: data
  55.                 });
  56.             }
  57.             else if(room.length >= 2) {
  58.  
  59.                 chat.emit('tooMany', {boolean: true});
  60.             }
  61.         });
  62.  
  63.         // When the client emits 'login', save his name and avatar,
  64.         // and add them to the room
  65.         socket.on('login', function(data) {
  66.  
  67.             var room = findClientsSocket(io, data.id);
  68.             // Only two people per room are allowed
  69.             if (room.length < 2) {
  70.  
  71.                 // Use the socket object to store data. Each client gets
  72.                 // their own unique socket object
  73.  
  74.                 socket.username = data.user;
  75.                 socket.room = data.id;
  76.                 socket.avatar = gravatar.url(data.avatar, {s: '140', r: 'x', d: 'mm'});
  77.  
  78.                 // Tell the person what he should use for an avatar
  79.                 socket.emit('img', socket.avatar);
  80.  
  81.  
  82.                 // Add the client to the room
  83.                 socket.join(data.id);
  84.  
  85.                 if (room.length == 1) {
  86.  
  87.                     var usernames = [],
  88.                         avatars = [];
  89.  
  90.                     usernames.push(room[0].username);
  91.                     usernames.push(socket.username);
  92.  
  93.                     avatars.push(room[0].avatar);
  94.                     avatars.push(socket.avatar);
  95.  
  96.                     // Send the startChat event to all the people in the
  97.                     // room, along with a list of people that are in it.
  98.  
  99.                     chat.in(data.id).emit('startChat', {
  100.                         boolean: true,
  101.                         id: data.id,
  102.                         users: usernames,
  103.                         avatars: avatars
  104.                     });
  105.                 }
  106.             }
  107.             else {
  108.                 socket.emit('tooMany', {boolean: true});
  109.             }
  110.         });
  111.  
  112.         // Somebody left the chat
  113.         socket.on('disconnect', function() {
  114.  
  115.             // Notify the other person in the chat room
  116.             // that his partner has left
  117.  
  118.             socket.broadcast.to(this.room).emit('leave', {
  119.                 boolean: true,
  120.                 room: this.room,
  121.                 user: this.username,
  122.                 avatar: this.avatar
  123.             });
  124.  
  125.             // leave the room
  126.             socket.leave(socket.room);
  127.         });
  128.  
  129.  
  130.         // Handle the sending of messages
  131.         socket.on('msg', function(data){
  132.  
  133.             // When the server receives a message, it sends it to the other person in the room.
  134.             socket.broadcast.to(socket.room).emit('receive', {msg: data.msg, user: data.user, img: data.img});
  135.         });
  136.     });
  137. };
  138.  
  139. function findClientsSocket(io,roomId, namespace) {
  140.     var res = [],
  141.         ns = io.of(namespace ||"/");    // the default namespace is "/"
  142.  
  143.     if (ns) {
  144.         for (var id in ns.connected) {
  145.             if(roomId) {
  146.                 var index = ns.connected[id].rooms.indexOf(roomId) ;
  147.                 if(index !== -1) {
  148.                     res.push(ns.connected[id]);
  149.                 }
  150.             }
  151.             else {
  152.                 res.push(ns.connected[id]);
  153.             }
  154.         }
  155.     }
  156.     return res;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement