var socket = io.connect('http://localhost');
socket.emit('join', {email: user1@example.com});
现在,从服务器端使用该信息为该用户创建一个唯一的房间
服务器端:
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.on('join', function (data) {
socket.join(data.email); // We are using room of socket io
});
});
In a project of our company we are using "rooms" approach and it's name is a combination of user ids of all users in a conversation as a unique identifier (our implementation is more like facebook messenger), example:
名字
斯科特
苏珊
"room" name will be "1-2" (ids are ordered Asc.) and on disconnect socket.io automatically cleans up the room