Created
February 14, 2016 11:24
-
-
Save johannesMatevosyan/0b9f7e588338dbb6b7f5 to your computer and use it in GitHub Desktop.
socket.io - keep track of clients
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
// client side code | |
var socket = io.connect('http://localhost:3000/'); | |
var chatInfra = io.connect('http://localhost:3000/chat_infra'), | |
chatCom = io.connect('http://localhost:3000/chat_com'); | |
chatInfra.on('name_set', function (data) { | |
chatInfra.on("user_entered", function (user) { | |
$('#messages').append('<div class="systemMessage">' + user.name + ' has joined the room.' + '</div>'); | |
}); | |
chatInfra.on('message', function (message) { | |
var message = JSON.parse(message); | |
$('#messages').append('<div class="' + message.type + '">' + message.message + '</div>'); | |
}); | |
chatCom.on('message', function (message) { | |
var message = JSON.parse(message); | |
$('#messages').append('<div class="' + message.type + '"><span class="name">' + message.username + ":</span> " + message.message + '</div>'); | |
}); | |
$('#nameform').hide(); | |
$('#messages').append('<div class="systemMessage">Hello ' + data.name + '</div>'); | |
$('#send').click(function () { | |
var data = { | |
message:$('#message').val(), | |
type:'userMessage' | |
}; | |
chatCom.send(JSON.stringify(data)); | |
$('#message').val(''); | |
}); | |
}); | |
$(function () { | |
$('#setname').click(function () { | |
// fetch logged in user's name and send it to the server. | |
chatInfra.emit("set_name", {name:$('#nickname').val()}); | |
}); | |
}); |
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
// Server side code | |
io = io.listen(server); | |
var clients = []; | |
var chatInfra = io.of("/chat_infra").on("connection", function(socket){ // fix logged in users | |
socket.on("set_name", function (data) { | |
clients.push(data.name); // store users into array | |
socket.emit('name_set', data); | |
socket.send(JSON.stringify({type:'serverMessage', message:'Welcome to the most interesting ' + 'chat room on earth!'})); | |
socket.broadcast.emit('user_entered', data); | |
}); | |
}); | |
var chatCom = io.of("/chat_com").on("connection", function (socket) { // handle messageing between users | |
socket.on('message', function (message) { | |
message = JSON.parse(message); | |
for(var key in clients){ // itarate over users | |
if(message.type == "userMessage"){ | |
message.username = clients[key]; | |
socket.broadcast.send(JSON.stringify(message)); | |
message.type = "myMessage"; | |
socket.send(JSON.stringify(message)); | |
} | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment