Created
February 20, 2017 21:05
-
-
Save hartct/f5abd288f000f45860e06f2505c49544 to your computer and use it in GitHub Desktop.
Socket.io connection manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(io, http) { | |
const channelSpecificMessageTypes = ['agent joined', 'channel message', 'customer message', 'agent message']; | |
io.on('connection', function(socket){ | |
console.log('a user connected'); | |
socket.on('chat message', function(msg){ | |
console.log('socketMgr: chat message: ' + msg); | |
io.emit('chat message', msg); | |
}); | |
channelSpecificMessageTypes.forEach(function(msgType) { | |
socket.on(msgType, function(msg){ | |
console.log('socketMgr: got msgType = ' + msgType + 'in room: ' + socket.room); | |
if (socket.room !== undefined) { | |
io.sockets.in(socket.room).emit(msgType, msg); | |
} | |
}); | |
}); | |
socket.on('join channel', function(msg){ | |
console.log("joining channel " + msg); | |
socket.room = msg; | |
socket.join(msg); | |
}); | |
socket.on('leave channel', function(msg){ | |
socket.leave(msg); | |
socket.room = null; | |
}); | |
socket.on('disconnect', function(){ | |
console.log('user disconnected'); | |
socket.room = null; | |
}); | |
}); | |
http.listen(3000, function(){ | |
console.log('listening on *:3000'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment