|
@@ -17,7 +17,9 @@ const EVENT = {
|
|
|
action: 'action',
|
|
|
roomClose: 'roomClose',
|
|
|
roomIn: 'roomIn',
|
|
|
- changeVoiceStatus: 'changeVoiceStatus'
|
|
|
+ changeVoiceStatus: 'changeVoiceStatus',
|
|
|
+ changeOnlineStatus: 'changeOnlineStatus',
|
|
|
+ roomPersonChange: 'roomPersonChange'
|
|
|
}
|
|
|
|
|
|
const CODEMAP = {
|
|
@@ -42,19 +44,19 @@ module.exports = class WebSocketServer {
|
|
|
pingTimeout: 5000,
|
|
|
cookie: false,
|
|
|
});
|
|
|
-
|
|
|
server.listen(3030, { origins: "*" });
|
|
|
|
|
|
this.io.on("connection", (socket) => {
|
|
|
let user = socket.handshake.query
|
|
|
const { roomId, userId, sceneNum, isClient, role, userLimitNum } = user
|
|
|
const webRoomId = `${roomId}_${sceneNum}_web`
|
|
|
- const clientRoom = userId ? `${userId}` : `${userId}${roomId}`
|
|
|
+ const clientRoom = `${userId}${roomId}`
|
|
|
+ console.log(user, 'connection')
|
|
|
// 加入客户端与H5单独的通讯房间
|
|
|
socket.join(clientRoom, () => {
|
|
|
})
|
|
|
-
|
|
|
socket.on(EVENT.clientSyncAction, (data) => {
|
|
|
+ console.log(data,'clientSyncAction')
|
|
|
socket.broadcast.to(clientRoom).emit(EVENT.clientSyncAction, data)
|
|
|
})
|
|
|
|
|
@@ -80,8 +82,6 @@ module.exports = class WebSocketServer {
|
|
|
return this
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
// 若已结束、房间已关闭
|
|
|
if ( user.role !== 'leader'&& ((this._roomPerson.get(roomId) && !this._roomPerson.get(roomId).find(item => item.role === 'leader')) || (!this._roomPerson.get(roomId)))) {
|
|
|
console.log('roomClose')
|
|
@@ -104,6 +104,8 @@ module.exports = class WebSocketServer {
|
|
|
if (item.userId === user.userId && item.avatar) {
|
|
|
user.avatar = item.avatar
|
|
|
user.voiceStatus = item.voiceStatus
|
|
|
+ user.onlineStatus = item.onlineStatus
|
|
|
+ user.nickname = decodeURIComponent(item.nickname)
|
|
|
}
|
|
|
})
|
|
|
roomsPerson.push(user)
|
|
@@ -132,19 +134,24 @@ module.exports = class WebSocketServer {
|
|
|
}
|
|
|
})
|
|
|
|
|
|
+ socket.on(EVENT.changeOnlineStatus, data => {
|
|
|
+ user.onlineStatus = data.status
|
|
|
+ let roomPerson = this._roomPerson.get(roomId)
|
|
|
+ let __roomPerson = updateRoomUser(roomPerson, user)
|
|
|
+ let actionName = user.onlineStatus ? 'inRoom' : 'leaveRoom'
|
|
|
+ console.log({roomsPerson: sortRoomUser(__roomPerson), actionName})
|
|
|
+ socket.broadcast.to(roomId).emit(EVENT.roomPersonChange, {roomsPerson: sortRoomUser(__roomPerson), actionName, user})
|
|
|
+ })
|
|
|
+
|
|
|
socket.on(EVENT.stopCall, () => {
|
|
|
socket.leave(roomId)
|
|
|
if (role === 'leader') {
|
|
|
socket.broadcast.to(roomId).emit(EVENT.roomClose, { code: 3002, msg: CODEMAP[3002]})
|
|
|
}
|
|
|
- if (!isClient) {
|
|
|
- console.log('stopCall', user)
|
|
|
- // socket.leave(webRoomId)
|
|
|
- let roomsPerson = this._roomPerson.get(roomId) || []
|
|
|
- roomsPerson = removeRoomUser(roomsPerson, user)
|
|
|
- this._roomPerson.set(roomId, roomsPerson)
|
|
|
- socket.broadcast.to(roomId).emit(EVENT.someOneLeaveRoom, { user, roomsPerson: sortRoomUser(roomsPerson) })
|
|
|
- }
|
|
|
+ let roomsPerson = this._roomPerson.get(roomId) || []
|
|
|
+ roomsPerson = removeRoomUser(roomsPerson, user)
|
|
|
+ this._roomPerson.set(roomId, roomsPerson)
|
|
|
+ socket.broadcast.to(roomId).emit(EVENT.someOneLeaveRoom, { user, roomsPerson: sortRoomUser(roomsPerson) })
|
|
|
})
|
|
|
|
|
|
socket.on(EVENT.webSyncAction, (data) => {
|
|
@@ -152,6 +159,7 @@ module.exports = class WebSocketServer {
|
|
|
})
|
|
|
|
|
|
socket.on(EVENT.action, (data) => {
|
|
|
+ console.log(data,'action')
|
|
|
socket.broadcast.to(roomId).emit(EVENT.action, data)
|
|
|
})
|
|
|
|
|
@@ -206,6 +214,18 @@ function removeRoomUser (roomsPerson, user) {
|
|
|
}
|
|
|
|
|
|
function sortRoomUser (roomsPerson) {
|
|
|
+ if (!roomsPerson) return []
|
|
|
const res = new Map()
|
|
|
return roomsPerson.filter((a) => !res.has(a.userId) && res.set(a.userId, 1))
|
|
|
}
|
|
|
+
|
|
|
+function updateRoomUser (roomsPerson, user) {
|
|
|
+ if (!roomsPerson) return []
|
|
|
+ roomsPerson.forEach(item => {
|
|
|
+ console.log(item.userId === user.userId)
|
|
|
+ if (item.userId === user.userId) {
|
|
|
+ item = Object.assign(item, user)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ return roomsPerson
|
|
|
+}
|