我试图通过构建一组动态创建的聊天室来学习Socket.io,这些聊天室会发出“连接”字样。并且'断开'用户进入和离开时的消息。在查看了couple of questions之后,我将一些功能性内容整合在一起,但大多数相关的回复来自于承认他们已将黑客一起攻击的人和我#&# 39;我注意到在Socket.io repo(尤其是here和here)
上有更正式的 - 也是最近的 - 关于正确方法的讨论由于我是这样的新手,我不知道下面的工作是否是一种可接受的做事方式,或者恰好偶然发挥功能,但会导致性能问题或导致听众太多。如果有一种理想的 - 官方的 - 加入和离开的房间感觉不那么笨重,我很乐意去了解它。
客户端
var roomId = ChatRoomData._id // comes from a factory
function init() {
// Make sure the Socket is connected
if (!Socket.socket) {
Socket.connect();
}
// Sends roomId to server
Socket.on('connect', function() {
Socket.emit('room', roomId);
});
// Remove the event listener when the controller instance is destroyed
$scope.$on('$destroy', function () {
Socket.removeListener('connect');
});
}
init();
服务器
io.sockets.once('connection', function(socket){
socket.on('room', function(room){ // take room variable from client side
socket.join(room) // and join it
io.sockets.in(room).emit('message', { // Emits a status message to the connect room when a socket client is connected
type: 'status',
text: 'Is now connected',
created: Date.now(),
username: socket.request.user.username
});
socket.on('disconnect', function () { // Emits a status message to the connected room when a socket client is disconnected
io.sockets.in(room).emit({
type: 'status',
text: 'disconnected',
created: Date.now(),
username: socket.request.user.username
});
})
});
答案 0 :(得分:3)
Socket.IO :最近发布 v2.0.3
加入房间就像socket.join('roomName'
一样简单
//:JOIN:Client Supplied Room
socket.on('subscribe',function(room){
try{
console.log('[socket]','join room :',room)
socket.join(room);
socket.to(room).emit('user joined', socket.id);
}catch(e){
console.log('[error]','join room :',e);
socket.emit('error','couldnt perform requested action');
}
})
和离开一个房间,简单为socket.leave('roomName');
:
//:LEAVE:Client Supplied Room
socket.on('unsubscribe',function(room){
try{
console.log('[socket]','leave room :', room);
socket.leave(room);
socket.to(room).emit('user left', socket.id);
}catch(e){
console.log('[error]','leave room :', e);
socket.emit('error','couldnt perform requested action');
}
})
Not able to get the list of rooms the client is currently in on disconnect event
Has been fixed (Add a 'disconnecting' event to access to socket.rooms upon disconnection)
socket.on('disconnect', function(){(
/*
socket.rooms is empty here
leaveAll() has already been called
*/
});
socket.on('disconnecting', function(){
// socket.rooms should isn't empty here
var rooms = socket.rooms.slice();
/*
here you can iterate over the rooms and emit to each
of those rooms where the disconnecting user was.
*/
});
现在发送到特定房间:
// sending to all clients in 'roomName' room except sender
socket.to('roomName').emit('event', 'content');
答案 1 :(得分:0)
这是我通知用户“断开用户连接”的方式
socket.on('disconnecting', function(){
console.log("disconnecting.. ", socket.id)
notifyFriendOfDisconnect(socket)
});
function notifyFriendOfDisconnect(socket){
var rooms = Object.keys(socket.rooms);
rooms.forEach(function(room){
socket.to(room).emit('connection left', socket.id + ' has left');
});
}
答案 2 :(得分:0)
对于在 2/1/2021 之后使用 socket.io 3.1.0 阅读本文的任何人,希望以后可以参考我的示例。我发现 socket.io 文档中有关如何执行此操作的示例不正确。他们声称在断开连接事件中 socket.rooms 是一个对象。虽然 is 使用块容器并以逗号分隔,但没有密钥对,这意味着它们的 const rooms = Object.keys(socket.rooms) 演示返回一个空值。它从一个实际上只是一个数组的对象中创建一个数组。据我所知,{} 只能用于块语句和对象。奇怪的是,NodeJS 将其视为普通数组。我在每个连接事件中分配自定义的 4 位数房间。所以我在断开连接事件中让服务器浏览所有房间,如果它遇到长度为 4 的房间名称,它会告诉房间中的每个人房间的大小减一。在客户端,我有一个套接字事件侦听器对此进行监视,当检测到它时,更新
的 innerHTML 属性,以便客户端可以显示已连接用户的数量。< /p>
//client side code:
socket.on('roomSize', (roomSize) => {
document.getElementById('clientCount').innerHTML = roomSize + ' clients connected';
});
//Server side code:
io.on("connection", (socket) => {
socket.on('createRoom', () => {
let ID = makeID(4)
while (io.sockets.adapter.rooms.has(ID)) {
ID = makeID(4)
}
socket.join(ID);
socket.emit('setID', ID);
});
socket.on("joinRoom", (room) => {
socket.join(room);
let roomSize = io.sockets.adapter.rooms.get(room).size
io.in(room).emit('roomSize', roomSize);
socket.emit('roomJoined', room, roomSize);
});
socket.on('disconnecting', function() {
let rooms = socket.rooms;
rooms.forEach(function(room) {
if (room.length === 4) {
let roomSize = io.sockets.adapter.rooms.get(room).size - 1
io.in(room).emit('roomSize', roomSize);
}
});
});
function makeID(length) {
var result = '';
var characters = '0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
})
答案 3 :(得分:0)
这是一种工作方法。
我在服务器端使用 socketio "socket.io": "^2.3.0"
。在客户端是android
// SocketIO
implementation('io.socket:socket.io-client:1.0.0') {
// excluding org.json which is provided by Android
exclude group: 'org.json', module: 'json'
}
以下是对我有用的代码。
// Join Chat Room
socket.on('ic_join', function(data) {
// Json Parse String To Access Child Elements
let messageJson = JSON.parse(data)
let room1 = messageJson.room1
console.log('======Joined Room========== ')
console.log(room1)
socket.join(room1, function(err) {
console.log(io.sockets.adapter.rooms[room1].length);
console.log(err)
})
})
// Leave Chat Room
socket.on('ic_leave', function(data) {
// Json Parse String To Access Child Elements
let messageJson = JSON.parse(data)
let room1 = messageJson.room1
console.log('======Left Room========== ')
console.log(room1)
socket.leave(room1, function(err) {
if (typeof io.sockets.adapter.rooms[room1] !== 'undefined' && io.sockets.adapter.rooms[room1] != null) {
console.log(io.sockets.adapter.rooms[room1].length);
console.log(err)
} else{
console.log("room is deleted")
}
})
})