Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

兼容其他版本socket io #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,30 @@ io.sockets.on('connection', function (socket) {
} else {
//向特定用户发送该用户发话信息
//clients 为存储所有连接对象的数组
var clients = io.sockets.clients();

var clients = io.sockets.clients();
//遍历找到该用户
clients.forEach(function (client) {
clients.forEach(function (client) {
if (client.name == data.to) {
//触发该用户客户端的 say 事件
client.emit('say', data);
}
});



/**
//当socket.io版本高于0.9时可以使用以下方法获取clients

var clients = io.sockets.sockets.map(function(e) {return e.name});

for (var i = 0; i < clients.length; i++) {
if(clients[i] == data.to) io.sockets.emit('say', data);
}
**/
}


});

//有人下线
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
},
"dependencies": {
"express": "3.3.1",
"socket.io": "*"
"socket.io": "*"
}
}