Skip to content

Commit

Permalink
feat: 完善task变动Hooks发送socket消息;
Browse files Browse the repository at this point in the history
完善task变动Hooks发送socket消息;
  • Loading branch information
Imfdj committed Mar 11, 2021
1 parent 8604a2e commit e854942
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
17 changes: 16 additions & 1 deletion app/extend/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,24 @@ module.exports = {
method,
params,
};
this.ctx.app.redis.set(this.redisKeys.socketBaseSocketId(data.id), JSON.stringify(data));
return data;
},
/**
* 发送socket消息给room里的每个连接,并录入redis
*/
sendSocketToClientOfRoom(params, action, roomName = `${ this.app.config.socketProjectRoomNamePrefix }${ params.project_id }`, method = 'publish') {
const { ctx, app, redisKeys } = this;
const nsp = app.io.of('/');
nsp.adapter.clients([roomName], (err, clients) => {
clients.forEach(clientId => {
const data = ctx.helper.parseSocketMsg(params, clientId, action, method);
const socket = nsp.to(clientId);
socket.emit('message', data);
// 存入redis,接收到ACK则删除,否则在 this.app.config.socketRedisExp 时间内多次重发
app.redis.setex(redisKeys.socketBaseSocketId(data.id), this.app.config.socketRedisExp, JSON.stringify(data));
});
});
},
};

module.exports.tools = {
Expand Down
33 changes: 3 additions & 30 deletions app/model/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,43 +44,16 @@ module.exports = app => {
likes: 0,
plan_work_hours: 0,
}, task.dataValues);
const roomName = `${ app.config.socketProjectRoomNamePrefix }${ task.project_id }`;
const nsp = app.io.of('/');
// app.io.of('/').to(roomName).emit(newTask); // broadcast to everyone in the room
nsp.adapter.clients([roomName], (err, clients) => {
clients.forEach(clientId => {
const data = ctx.helper.parseSocketMsg(newTask, clientId, 'create:task');
const socket = nsp.to(clientId);
socket.emit('message', data);
});
});
ctx.helper.sendSocketToClientOfRoom(newTask, 'create:task');
});
task.addHook('afterUpdate', async (task, options) => {
const ctx = await app.createAnonymousContext();
const roomName = `${ app.config.socketProjectRoomNamePrefix }${ task.project_id }`;
const nsp = app.io.of('/');
nsp.adapter.clients([roomName], (err, clients) => {
clients.forEach(clientId => {
const data = ctx.helper.parseSocketMsg(task, clientId, 'update:task');
const socket = nsp.to(clientId);
socket.emit('message', data);
});
});
ctx.helper.sendSocketToClientOfRoom(task, 'update:task');
});

task.addHook('afterDestroy', async (task, options) => {
const ctx = await app.createAnonymousContext();
const roomName = `${ app.config.socketProjectRoomNamePrefix }${ task.project_id }`;
const nsp = app.io.of('/');
nsp.adapter.clients([roomName], (err, clients) => {
clients.forEach(clientId => {
const data = ctx.helper.parseSocketMsg(task, clientId, 'delete:task');
const socket = nsp.to(clientId);
socket.emit('message', data);
});
});
ctx.helper.sendSocketToClientOfRoom(task, 'delete:task');
});

task.associate = function(models) {
// associations can be defined here
task.hasOne(app.model.Users, { foreignKey: 'id', sourceKey: 'executor_id', as: 'executor' });
Expand Down
1 change: 1 addition & 0 deletions app/service/user_tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class _objectName_Service extends Service {
executor_id: user_id,
},
transaction,
individualHooks: true,
});
await transaction.commit();
return res;
Expand Down

0 comments on commit e854942

Please sign in to comment.