|
@@ -15,7 +15,7 @@ export class RoomService {
|
|
|
private readonly userService: UsersService,
|
|
|
private readonly actionsService: ActionsService,
|
|
|
private readonly delayService: DelayService,
|
|
|
- ) {}
|
|
|
+ ) { }
|
|
|
public readonly logger = new Logger('user');
|
|
|
public _sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
|
public _userInfo = {} as UserInfoType;
|
|
@@ -67,15 +67,29 @@ export class RoomService {
|
|
|
|
|
|
async handleUserOffline(socket: Socket) {
|
|
|
await this._sleep(500);
|
|
|
- if (socket.data.user) {
|
|
|
- socket.data.user.IsOnline = false;
|
|
|
- await this.userService.updateUserOnlineState(
|
|
|
- socket,
|
|
|
- socket.data.user.RoomId,
|
|
|
- socket.data.user.UserId,
|
|
|
- false,
|
|
|
- );
|
|
|
- await this.handleRoomStatusAction(socket);
|
|
|
+ if (socket.data?.user) {
|
|
|
+ this.delayService.handleOffline(socket);
|
|
|
+ const { RoomId, UserId } = socket.data.user;
|
|
|
+ if (RoomId && UserId) {
|
|
|
+ //标记主动退出房间
|
|
|
+ if (socket.data.isRequestExit !== 1) {
|
|
|
+ const roomUsers = await this.userService.getRoomUsers(RoomId);
|
|
|
+ this.socketGateway.server.to(RoomId).emit('action', {
|
|
|
+ type: 'user-leave',
|
|
|
+ user: socket.data.user,
|
|
|
+ members: roomUsers,
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ socket.data.user.IsOnline = false;
|
|
|
+ await this.userService.updateUserOnlineState(
|
|
|
+ socket,
|
|
|
+ socket.data.user.RoomId,
|
|
|
+ socket.data.user.UserId,
|
|
|
+ false,
|
|
|
+ );
|
|
|
+ await this.handleRoomStatusAction(socket);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -91,9 +105,7 @@ export class RoomService {
|
|
|
this.initUserProfile(userInfo);
|
|
|
socket.data.user = this._userInfo;
|
|
|
await this.handleUserOnline(socket);
|
|
|
- //test
|
|
|
- this.delayService.handleOffline();
|
|
|
-
|
|
|
+ await this.delayService.handleOnine(socket);
|
|
|
let blockJoin = false;
|
|
|
if (this._roomId?.length && this._userId?.length) {
|
|
|
//房主设置房间配置
|
|
@@ -110,11 +122,12 @@ export class RoomService {
|
|
|
);
|
|
|
} else {
|
|
|
blockJoin = true;
|
|
|
- socket.emit('action', {
|
|
|
+ socket.emit('manager-error', {
|
|
|
type: 'invalid-master',
|
|
|
code: 303,
|
|
|
});
|
|
|
this.logger.warn(`303:invalid-master`, 'join-error');
|
|
|
+ socket.disconnect(true);
|
|
|
}
|
|
|
}
|
|
|
const isExist = await this.userService.isUserInRooms(
|
|
@@ -128,14 +141,15 @@ export class RoomService {
|
|
|
} else {
|
|
|
const updated = await this.userService.updateUsers(this._userInfo);
|
|
|
if (!updated) {
|
|
|
- socket.emit('action', {
|
|
|
- type: 'error',
|
|
|
+ socket.emit('manager-error', {
|
|
|
+ type: 'invalid-match-role',
|
|
|
code: 405,
|
|
|
});
|
|
|
this.logger.warn(
|
|
|
`same userId in room not matchRole `,
|
|
|
'join-error',
|
|
|
);
|
|
|
+ socket.disconnect(true);
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
@@ -158,8 +172,13 @@ export class RoomService {
|
|
|
);
|
|
|
}
|
|
|
} else {
|
|
|
- socket.emit('action', { type: 'error', code: 403 });
|
|
|
+ socket.emit('manager-error', {
|
|
|
+ type: 'invalid-room-params',
|
|
|
+ code: 403,
|
|
|
+ });
|
|
|
this.logger.warn(`403:not roomId and userId`, 'join-error');
|
|
|
+ socket.disconnect(true);
|
|
|
+ return;
|
|
|
}
|
|
|
}
|
|
|
/**
|
|
@@ -191,7 +210,7 @@ export class RoomService {
|
|
|
* @param message
|
|
|
*/
|
|
|
async handleKickAction(socket: Socket, RoomId: string, userId: string) {
|
|
|
- const delUser = await this.userService.getUsersByUserId(RoomId, userId);
|
|
|
+ const delUser = await this.userService.getUsersBy(RoomId, userId);
|
|
|
this.logger.warn(
|
|
|
`RoomId: ${RoomId},userId:${userId} socketId:${delUser.id}`,
|
|
|
'kick-user',
|
|
@@ -209,7 +228,7 @@ export class RoomService {
|
|
|
const res = await this.userService.deleteRoomUser(RoomId, userId);
|
|
|
if (res) {
|
|
|
this.socketGateway.server.to(RoomId).emit('action', {
|
|
|
- type: 'user-leave',
|
|
|
+ type: 'user-exit',
|
|
|
user: delUser,
|
|
|
members: filterRoomUser,
|
|
|
});
|
|
@@ -233,15 +252,15 @@ export class RoomService {
|
|
|
* @param message
|
|
|
*/
|
|
|
async handleExitAction(socket: Socket, message: any) {
|
|
|
- console.log('socket', socket.data.user);
|
|
|
const roomId = message?.roomId || socket.data.user?.RoomId;
|
|
|
const userId = message?.userId || socket.data.user?.UserId;
|
|
|
if (roomId && userId) {
|
|
|
- const delUser = await this.userService.getUsersByUserId(roomId, userId);
|
|
|
+ const delUser = await this.userService.getUsersBy(roomId, userId);
|
|
|
this.logger.warn(
|
|
|
`RoomId: ${roomId},userId:${userId} socketId:${delUser.id}`,
|
|
|
'room-exit',
|
|
|
);
|
|
|
+ socket.data.isRequestExit = 1;
|
|
|
const roomUsers = await this.userService.getRoomUsers(roomId);
|
|
|
const filterRoomUser = roomUsers.filter((i) => i.UserId !== userId);
|
|
|
const res = await this.userService.deleteRoomUser(roomId, userId);
|
|
@@ -251,7 +270,7 @@ export class RoomService {
|
|
|
this.handleRoomDismiss(roomId);
|
|
|
} else {
|
|
|
this.socketGateway.server.to(roomId).emit('action', {
|
|
|
- type: 'user-leave',
|
|
|
+ type: 'user-exit',
|
|
|
user: delUser,
|
|
|
members: filterRoomUser,
|
|
|
});
|
|
@@ -264,11 +283,14 @@ export class RoomService {
|
|
|
* 解散房间
|
|
|
*/
|
|
|
|
|
|
- async handleRoomDismiss(RoomId: string): Promise<void> {
|
|
|
+ async handleRoomDismiss(RoomId: string): Promise<boolean> {
|
|
|
await this.userService.delRoom(RoomId);
|
|
|
await this.userService.delRoomConfig(RoomId);
|
|
|
+ await this.userService.delRoomMsgs(RoomId);
|
|
|
this.socketGateway.server
|
|
|
.to(RoomId)
|
|
|
.emit('action', { type: 'leader-dismiss' });
|
|
|
+ return Promise.resolve(true);
|
|
|
}
|
|
|
+
|
|
|
}
|