屏幕共享优化
This commit is contained in:
@@ -92,15 +92,33 @@ export class WatchRoomServer {
|
||||
}
|
||||
|
||||
const userId = socket.id;
|
||||
let isOwner = false;
|
||||
|
||||
if (data.ownerToken && data.ownerToken === room.ownerToken) {
|
||||
isOwner = true;
|
||||
room.ownerId = userId;
|
||||
room.lastOwnerHeartbeat = Date.now();
|
||||
this.rooms.set(data.roomId, room);
|
||||
console.log(`[WatchRoom] Owner ${data.userName} reconnected to room ${data.roomId}`);
|
||||
}
|
||||
|
||||
const member: Member = {
|
||||
id: userId,
|
||||
name: data.userName,
|
||||
isOwner: false,
|
||||
isOwner,
|
||||
lastHeartbeat: Date.now(),
|
||||
};
|
||||
|
||||
const roomMembers = this.members.get(data.roomId);
|
||||
if (roomMembers) {
|
||||
if (isOwner) {
|
||||
Array.from(roomMembers.entries()).forEach(([memberId, existingMember]) => {
|
||||
if (existingMember.isOwner && memberId !== userId) {
|
||||
roomMembers.delete(memberId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
roomMembers.set(userId, member);
|
||||
room.memberCount = roomMembers.size;
|
||||
this.rooms.set(data.roomId, room);
|
||||
@@ -110,7 +128,7 @@ export class WatchRoomServer {
|
||||
roomId: data.roomId,
|
||||
userId,
|
||||
userName: data.userName,
|
||||
isOwner: false,
|
||||
isOwner,
|
||||
});
|
||||
|
||||
socket.join(data.roomId);
|
||||
@@ -118,7 +136,7 @@ export class WatchRoomServer {
|
||||
// 通知房间内其他成员
|
||||
socket.to(data.roomId).emit('room:member-joined', member);
|
||||
|
||||
console.log(`[WatchRoom] User ${data.userName} joined room ${data.roomId}`);
|
||||
console.log(`[WatchRoom] User ${data.userName} joined room ${data.roomId}${isOwner ? ' (as owner)' : ''}`);
|
||||
|
||||
const members = Array.from(roomMembers?.values() || []);
|
||||
callback({ success: true, room, members });
|
||||
|
||||
@@ -13,6 +13,7 @@ export type WatchRoomSocket = Socket<ServerToClientEvents, ClientToServerEvents>
|
||||
class WatchRoomSocketManager {
|
||||
private socket: WatchRoomSocket | null = null;
|
||||
private config: WatchRoomConfig | null = null;
|
||||
private connectionPromise: Promise<WatchRoomSocket> | null = null;
|
||||
private heartbeatInterval: NodeJS.Timeout | null = null;
|
||||
private heartbeatTimeoutCheck: NodeJS.Timeout | null = null;
|
||||
private lastHeartbeatResponse: number = Date.now();
|
||||
@@ -25,6 +26,37 @@ class WatchRoomSocketManager {
|
||||
return this.socket;
|
||||
}
|
||||
|
||||
if (this.connectionPromise) {
|
||||
return this.connectionPromise;
|
||||
}
|
||||
|
||||
if (this.socket) {
|
||||
this.connectionPromise = new Promise((resolve, reject) => {
|
||||
const timeout = setTimeout(() => {
|
||||
this.connectionPromise = null;
|
||||
reject(new Error('Socket connection timeout'));
|
||||
}, 10000);
|
||||
|
||||
this.socket!.once('connect', () => {
|
||||
clearTimeout(timeout);
|
||||
this.connectionPromise = null;
|
||||
resolve(this.socket!);
|
||||
});
|
||||
|
||||
this.socket!.once('connect_error', (error) => {
|
||||
clearTimeout(timeout);
|
||||
this.connectionPromise = null;
|
||||
reject(error);
|
||||
});
|
||||
|
||||
if (!this.socket!.connected) {
|
||||
this.socket!.connect();
|
||||
}
|
||||
});
|
||||
|
||||
return this.connectionPromise;
|
||||
}
|
||||
|
||||
this.config = config;
|
||||
|
||||
const socketOptions = {
|
||||
@@ -72,8 +104,9 @@ class WatchRoomSocketManager {
|
||||
// 设置浏览器可见性监听
|
||||
this.setupVisibilityListener();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.connectionPromise = new Promise((resolve, reject) => {
|
||||
if (!this.socket) {
|
||||
this.connectionPromise = null;
|
||||
reject(new Error('Socket not initialized'));
|
||||
return;
|
||||
}
|
||||
@@ -82,6 +115,7 @@ class WatchRoomSocketManager {
|
||||
this.socket.once('connect', () => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('[WatchRoom] Connected to server');
|
||||
this.connectionPromise = null;
|
||||
if (this.socket) {
|
||||
resolve(this.socket);
|
||||
}
|
||||
@@ -90,9 +124,12 @@ class WatchRoomSocketManager {
|
||||
this.socket.once('connect_error', (error) => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error('[WatchRoom] Connection error:', error);
|
||||
this.connectionPromise = null;
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
|
||||
return this.connectionPromise;
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
@@ -122,6 +159,8 @@ class WatchRoomSocketManager {
|
||||
this.socket.disconnect();
|
||||
this.socket = null;
|
||||
}
|
||||
|
||||
this.connectionPromise = null;
|
||||
}
|
||||
|
||||
getSocket(): WatchRoomSocket | null {
|
||||
|
||||
Reference in New Issue
Block a user