|
| 1 | +var socketio = require('socket.io') |
| 2 | + , guestNumber = 1 |
| 3 | + , nickNames = {} |
| 4 | + , namesUsed = [] |
| 5 | + , currentRoom = {}; |
| 6 | + |
| 7 | +exports.listen = function(server) { |
| 8 | + io = socketio.listen(server); |
| 9 | + io.set('log level', 1); |
| 10 | + |
| 11 | + io.sockets.on('connection', function (socket) { |
| 12 | + guestNumber = assignGuestName(socket, guestNumber, nickNames, namesUsed); |
| 13 | + joinRoom(socket, 'Lobby'); |
| 14 | + |
| 15 | + handleMessageBroadcasting(socket, nickNames); |
| 16 | + handleNameChangeAttempts(socket, nickNames, namesUsed); |
| 17 | + handleRoomJoining(socket); |
| 18 | + |
| 19 | + socket.on('rooms', function() { |
| 20 | + socket.emit('rooms', io.sockets.manager.rooms); |
| 21 | + }); |
| 22 | + |
| 23 | + handleClientDisconnection(socket, nickNames, namesUsed); |
| 24 | + }); |
| 25 | +}; |
| 26 | + |
| 27 | +function assignGuestName(socket, guestNumber, nickNames, namesUsed) { |
| 28 | + var name = 'Guest' + guestNumber; |
| 29 | + nickNames[socket.id] = name; |
| 30 | + socket.emit('nameResult', { |
| 31 | + success: true, |
| 32 | + name: name |
| 33 | + }); |
| 34 | + namesUsed.push(name); |
| 35 | + return guestNumber + 1; |
| 36 | +} |
| 37 | + |
| 38 | +function joinRoom(socket, room) { |
| 39 | + socket.join(room); |
| 40 | + currentRoom[socket.id] = room; |
| 41 | + socket.emit('joinResult', {room: room}); |
| 42 | + socket.broadcast.to(room).emit('message', { |
| 43 | + text: nickNames[socket.id] + ' has joined ' + room + '.' |
| 44 | + }); |
| 45 | + |
| 46 | + var usersInRoom = io.sockets.clients(room); |
| 47 | + if (usersInRoom.length > 1) { |
| 48 | + var usersInRoomSummary = 'Users currently in ' + room + ': '; |
| 49 | + for (var index in usersInRoom) { |
| 50 | + var userSocketId = usersInRoom[index].id; |
| 51 | + if (userSocketId != socket.id) { |
| 52 | + if (index > 0) { |
| 53 | + usersInRoomSummary += ', '; |
| 54 | + } |
| 55 | + usersInRoomSummary += nickNames[userSocketId]; |
| 56 | + } |
| 57 | + } |
| 58 | + usersInRoomSummary += '.'; |
| 59 | + socket.emit('message', {text: usersInRoomSummary}); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function handleMessageBroadcasting(socket, nickNames) { |
| 64 | + socket.on('message', function (message) { |
| 65 | + socket.broadcast.to(message.room).emit('message', { |
| 66 | + text: nickNames[socket.id] + ': ' + message.text |
| 67 | + }); |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +function handleNameChangeAttempts(socket, nickNames, namesUsed) { |
| 72 | + socket.on('nameAttempt', function(name) { |
| 73 | + if (name.indexOf('Guest') == 0) { |
| 74 | + socket.emit('nameResult', { |
| 75 | + success: false, |
| 76 | + message: 'Names cannot begin with "Guest".' |
| 77 | + }); |
| 78 | + } else { |
| 79 | + if (namesUsed.indexOf(name) == -1) { |
| 80 | + var previousName = nickNames[socket.id]; |
| 81 | + var previousNameIndex = namesUsed.indexOf(previousName); |
| 82 | + namesUsed.push(name); |
| 83 | + nickNames[socket.id] = name; |
| 84 | + delete namesUsed[previousNameIndex]; |
| 85 | + socket.emit('nameResult', { |
| 86 | + success: true, |
| 87 | + name: name |
| 88 | + }); |
| 89 | + socket.broadcast.to(currentRoom[socket.id]).emit('message', { |
| 90 | + text: previousName + ' is now known as ' + name + '.' |
| 91 | + }); |
| 92 | + } else { |
| 93 | + socket.emit('nameResult', { |
| 94 | + success: false, |
| 95 | + message: 'That name is already in use.' |
| 96 | + }); |
| 97 | + } |
| 98 | + } |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +function handleRoomJoining(socket) { |
| 103 | + socket.on('join', function(room) { |
| 104 | + socket.leave(currentRoom[socket.id]); |
| 105 | + joinRoom(socket, room.newRoom); |
| 106 | + }); |
| 107 | +} |
| 108 | + |
| 109 | +function handleClientDisconnection(socket, nickNames, namesUsed) { |
| 110 | + socket.on('disconnect', function() { |
| 111 | + var nameIndex = namesUsed.indexOf(nickNames[socket.id]); |
| 112 | + delete namesUsed[nameIndex]; |
| 113 | + delete nickNames[socket.id]; |
| 114 | + }); |
| 115 | +} |
0 commit comments