Skip to content

Commit

Permalink
feat: 将创建者加入新创建的项目room,添加创建项目socket sycn;
Browse files Browse the repository at this point in the history
将创建者加入新创建的项目room,添加创建项目socket sycn;
  • Loading branch information
Imfdj committed Mar 17, 2021
1 parent 49b7229 commit 7042fad
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 25 deletions.
48 changes: 33 additions & 15 deletions app/extend/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,36 @@ module.exports = {
sendSocketToClientOfRoom(params, action, project_id = params.project_id, messageType = 'sync', method = 'publish') {
const { ctx, app, redisKeys } = this;
const nsp = app.io.of('/');
const roomName = `${ this.app.config.socketProjectRoomNamePrefix }${ project_id }`;
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), this.app.config.socketRedisExp, JSON.stringify(emitData));
const roomName = `${this.app.config.socketProjectRoomNamePrefix}${project_id}`;
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), this.app.config.socketRedisExp, JSON.stringify(emitData));
});
});
});
} catch (e) {
app.logger.error(e);
}
},
/**
* 给单个socket发送消息,并录入redis
*/
sendMessageToSocket(socket, params, action, messageType = 'sync', method = 'publish') {
const { ctx, app, redisKeys } = this;
try {
const _message = ctx.helper.parseSocketMsg(params, socket.id, action, method);
const emitData = [messageType, _message];
socket.emit(...emitData);
// 存入redis,接收到ACK则删除,否则在 this.app.config.socketRedisExp 时间内多次重发
app.redis.setex(redisKeys.socketBaseSocketId(_message.id), app.config.socketRedisExp, JSON.stringify(emitData));
} catch (e) {
app.logger.error(e);
}
},
};

Expand Down Expand Up @@ -132,7 +151,6 @@ module.exports.tools = {
query,
};
},

};

module.exports.body = {
Expand Down Expand Up @@ -222,18 +240,18 @@ module.exports.body = {
module.exports.redisKeys = {
// 资源基于action和url存储到redis中的key
permissionsBaseActionUrl(action = '', url = '') {
return `permissions:action:${ action }:url:${ url }`;
return `permissions:action:${action}:url:${url}`;
},
// 角色资源基于roleId存储到redis中的key
rolePermissionsBaseRoleId(id = '') {
return `rolePermissions:roleId:${ id }`;
return `rolePermissions:roleId:${id}`;
},
// 用户拥有的所有角色id,基于userId存储到redis中的key
userRoleIdsBaseUserId(id = '') {
return `userRoleIds:userId:${ id }`;
return `userRoleIds:userId:${id}`;
},
// socket发送后基于ID存储到redis中的key
socketBaseSocketId(id = '') {
return `socket:Id:${ id }`;
return `socket:Id:${id}`;
},
};
19 changes: 9 additions & 10 deletions app/model/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,21 @@ module.exports = app => {
message.addHook('afterCreate', async (message, options) => {
const ctx = await app.createAnonymousContext();
// 发送socket消息
const newMessage = Object.assign({
is_read: 0,
url: '',
}, message.dataValues);
const newMessage = Object.assign(
{
is_read: 0,
url: '',
},
message.dataValues
);
const nsp = app.io.of('/');
const { receiver_id, type } = message;
nsp.clients((error, clients) => {
if (error) throw error;
// 当此用户在线,则发送消息
if (clients.includes(receiver_id.toString())) {
const socket = nsp.to(receiver_id);
const _message = ctx.helper.parseSocketMsg(newMessage, receiver_id, type);
const emitData = ['message', _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));
const socket = nsp.sockets[receiver_id];
ctx.helper.sendMessageToSocket(socket, newMessage, type, 'message');
}
});
});
Expand Down
31 changes: 31 additions & 0 deletions app/model/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,37 @@ module.exports = app => {
{}
);

project.addHook('afterCreate', async (project, options) => {
const ctx = await app.createAnonymousContext();
// 发送socket消息
const { id, manager_id } = project;
const creator = await ctx.model.Users.findOne({ where: { id: manager_id } });
const newProject = Object.assign(
{
collector: [],
parent_id: 0,
progress: 0,
cover: '',
is_private: 1,
is_auto_progress: 0,
is_recycle: 0,
is_archived: 0,
state: 1,
intro: '',
creator,
},
project.dataValues
);
const nsp = app.io.of('/');
const roomName = `${app.config.socketProjectRoomNamePrefix}${id}`;
const socket = nsp.sockets[manager_id];
// 将创建者加入新创建的项目room
socket &&
socket.join(roomName, () => {
ctx.helper.sendMessageToSocket(socket, newProject, 'create:project');
});
});

project.addHook('afterUpdate', async (project, options) => {
const ctx = await app.createAnonymousContext();
ctx.helper.sendSocketToClientOfRoom(project, 'update:project', project.id);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
},
"lint-staged": {
"*.js": [
"pretty-quick --staged",
"eslint --fix --ext .js"
]
},
Expand Down

0 comments on commit 7042fad

Please sign in to comment.