admin管理员组文章数量:1314273
I'm building simple web application using HTML5 and Javascript on the front end and Node.js on the back end. I'm also using Socket.IO to pass data back and forth between the client and server. Say I have a chat page where everyone signs in and joins a chat room. I keep track of all the people in the chat room on the back end. What I want to do is remove a person from a room if they exit that room. For example say someone closes their browser, they should no longer be shown as an active user.
Socket.IO has a disconnect event which seems to be the perfect tool to do this. For example when I run this code:
socket.on("disconnect", function (){
console.log("DISCONNECTED");
});
"DISCONNECTED" will be printed when someone closes their browser. Now my question is, how am I supposed to know who exactly closed that browser? No data is passed to the disconnect event, so I'm not sure how exactly I'm supposed to check who set off the disconnect event. Any help would be greatly appreciated!
I'm building simple web application using HTML5 and Javascript on the front end and Node.js on the back end. I'm also using Socket.IO to pass data back and forth between the client and server. Say I have a chat page where everyone signs in and joins a chat room. I keep track of all the people in the chat room on the back end. What I want to do is remove a person from a room if they exit that room. For example say someone closes their browser, they should no longer be shown as an active user.
Socket.IO has a disconnect event which seems to be the perfect tool to do this. For example when I run this code:
socket.on("disconnect", function (){
console.log("DISCONNECTED");
});
"DISCONNECTED" will be printed when someone closes their browser. Now my question is, how am I supposed to know who exactly closed that browser? No data is passed to the disconnect event, so I'm not sure how exactly I'm supposed to check who set off the disconnect event. Any help would be greatly appreciated!
Share Improve this question asked Apr 25, 2013 at 7:38 Ben GoldbergBen Goldberg 514 bronze badges3 Answers
Reset to default 5Basically, once you listen the 'connect' event, you will get a socket object whenever a client connect to the server. And you can store client info to that socket object. You can refer to this article, and pay attention to the server side code, where the coder use pluarl form 'sockets' on server side (so server side will create many client socket!). http://psitsmike./2011/10/node-js-and-socket-io-multiroom-chat-tutorial/
hope the following sample will help you.
//server side
var io = require('socket.io').listen(80)
// sockets here, not socket
io.sockets.on('connection', function(socket){
// every connection will create a socket
socket.on('newUser', function(name){
socket.id = name; // socket stores the username
});
socket.on('disconnect', function(){
console.log(socket.id); // retrieve it from socket object
});
// more functionality goes...
});
and the client side
var socket = io.connect('http://localhost:80');
socket.on('connect', function(){
//emit newUser event so that client info is stored
socket.emit('newUser', prompt('Please enter your name!'));
//.... more functionality goes
});
Hope this will help you understand!
you could do:
socket.on("disconnect", function (){
console.log(this.id); // this.id is the 'id' of the socket that got disconnected
});
Your question makes no sense. Each socket is a connection to a single client.
Here's how you can associate a name with a socket, and then print the same name when the same socket disconnects:
io.sockets.on('connection', function(socket) {
var name = "";
socket.on('set-name', function(_name) {
name = _name;
});
socket.on("disconnect", function (){
console.log("DISCONNECTED "+name);
});
});
You already know which connection is being lost, since you hold the connection object socket
本文标签: javascriptSocketIOHow to figure out who disconnectedStack Overflow
版权声明:本文标题:javascript - Socket.IO - How to figure out who disconnected? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741965411a2407515.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论