IM/src/socket/handler/channel_chat.js

71 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const {socketClientEvent} = require("../event");
const userSessionService = require('../../service/user/tl_user_session_service')
const channelUserService = require('../../service/channel/tl_channel_user_service')
const { fields: channelUserFields } = require('../../tables/tl_channel_user')
const { Def: TlChannelUserDef } = channelUserFields
/**
* 广播聊天消息
* @param {*} io socketio对象
* @param {*} socket 单个socket连接
* @param {*} data event参数
* @returns
*/
async function channelChat(io, socket, data){
let channel = data.channel
let messageInfo = data.messageInfo
let to = data.to
const token = socket.token;
if(!channel){
return
}
channel = parseInt(channel)
const {
loginUserId, loginUserCompanyId
} = await userSessionService.getUserInfoByToken({token})
if(!loginUserId){
return
}
// 指定人,无须频道广播
if (to){
const toClient = io.sockets.connected[to]
if (toClient){
toClient.emit(socketClientEvent.channelChat, {
channel: data.channel,
messageInfo: messageInfo,
from: socket.id,
})
}
return
}
// 拿到当前频道下的所有用户socketid,进行广播
const channelUserList = await channelUserService.getListByChannelId({
channelId: channel,
companyId: loginUserCompanyId
}, [
TlChannelUserDef.userId
])
const userIds = channelUserList.map(item => item[TlChannelUserDef.userId])
// 广播给频道内的所有用户,但排除当前发送 socket,保留同账号其他终端
for(let userId of userIds){
socket.to(userId).emit(socketClientEvent.channelChat, {
channel: data.channel,
messageInfo: messageInfo,
from: socket.id,
})
}
}
module.exports = {
channelChat
}