Skip to content

Commit

Permalink
feat: 增加线用户room,提供online、join、leave同步socket指令;
Browse files Browse the repository at this point in the history
增加线用户room,提供online、join、leave同步socket指令;
  • Loading branch information
Imfdj committed May 19, 2021
1 parent d2e6a3c commit c927b5a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
21 changes: 21 additions & 0 deletions app/extend/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ module.exports = {
app.logger.errorAndSentry(e);
}
},
/**
* 发送socket消息给在线room里的每个连接,并录入redis
*/
sendSocketToOnlineOfRoom(params, action, roomName, messageType = 'sync', method = 'publish') {
const { ctx, app, redisKeys } = this;
const nsp = app.io.of('/');
try {
nsp.adapter.clients([roomName], (err, clients) => {
clients.forEach(clientId => {
const data = ctx.helper.parseSocketMsg(params, clientId, action, method);
const socket = nsp.to(clientId);
const emitData = [messageType, data];
socket.emit(...emitData);
// 存入redis,接收到ACK则删除,否则在 this.app.config.socketRedisExp 时间内多次重发
app.redis.setex(redisKeys.socketBaseSocketId(data.id), app.config.socketRedisExp, JSON.stringify(emitData));
});
});
} catch (e) {
app.logger.errorAndSentry(e);
}
},
/**
* 给单个socket发送消息,并录入redis
*/
Expand Down
33 changes: 29 additions & 4 deletions app/io/middleware/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module.exports = app => {
return async (ctx, next) => {
const { socket, logger } = ctx;
const { socketOnlineUserRoomName } = app.config;
const nsp = app.io.of('/');
console.log('start connection!');
console.log('allSockets');
Expand All @@ -18,7 +19,27 @@ module.exports = app => {
socket.disconnect();
}
await app.jwt.verify(token, app.config.jwt.secret);

// 加入在线用户room
socket.join(socketOnlineUserRoomName, () => {
setTimeout(() => {
// 给已在线用户发送join
// nsp.to(socketOnlineUserRoomName)
// .emit('join', socket.id);
ctx.helper.sendSocketToOnlineOfRoom({ socketId: socket.id }, 'join', socketOnlineUserRoomName);
nsp.adapter.clients([socketOnlineUserRoomName], (err, clients) => {
if (err) logger.error(err);
const ids = new Set(clients);
ids.add(socket.id);
// 发送当前在线用户ids
const _message = ctx.helper.parseSocketMsg(Array.from(ids), socket.id, 'online');
const emitData = ['sync', _message];
socket.emit(...emitData);
// 存入redis,接收到ACK则删除,否则在 this.app.config.socketRedisExp 时间内多次重发
app.redis.setex(ctx.helper.redisKeys.socketBaseSocketId(_message.id), app.config.socketRedisExp, JSON.stringify(emitData));
// socket.emit('online', Array.from(ids));
});
});
});
// 获取用户参与的项目,根据项目ID创建room
const userProjects = await ctx.model.UserProjects.findAll({
where: { user_id: userId },
Expand Down Expand Up @@ -52,8 +73,12 @@ module.exports = app => {
await next();
console.log('disconnect!');
console.log(socket.id);
// nsp.adapter.rooms.forEach(room => {
// socket.leave(room);
// });
socket.join(socketOnlineUserRoomName, () => {
setTimeout(() => {
ctx.helper.sendSocketToOnlineOfRoom({ socketId: socket.id }, 'leave', socketOnlineUserRoomName);
// nsp.to(socketOnlineUserRoomName)
// .emit('leave', socket.id);
});
});
};
};

0 comments on commit c927b5a

Please sign in to comment.