I am trying to write simple nodejs socket program to display the list of user connected. I am having issues keeping username and socket.id mapped.
Here is server side code:
app.post('/login', function(req, res){
var user = {
id: socket.id,//how to get this socket.id?
username: req.body.username,
loggedInTime: new Date()
};
connectionsArray.push(user);
res.render('main');
});
io.on('connection', function(socket){
console.log('Connecting user ' + socket.id);
if (connectionsArray.length > 0) {
setInterval(function(){
io.sockets.emit('connectionsArray', connectionsArray);
},1000);
}
socket.on('disconnect', function() {
connectionsArray = connectionsArray.map(function(conn){
return conn.id != socket.id;
});
console.log('user disconnected with' + socket.id);
});
});
via Rajeev Jayaswal
No comments:
Post a Comment