Commit 4554bc34 by zhangxingmin

push

parent 77f7d188
...@@ -83,6 +83,13 @@ public class CoWebSocketServer { ...@@ -83,6 +83,13 @@ public class CoWebSocketServer {
*/ */
private static final Map<Session, String> SESSION_USER_TYPE = new ConcurrentHashMap<>(); private static final Map<Session, String> SESSION_USER_TYPE = new ConcurrentHashMap<>();
/**
* 房间 -> 当前投屏者用户类型
* key: 房间号 (roomId)
* value: "owner" 或 "participant"
*/
private static final Map<String, String> PRESENTER_MAP = new ConcurrentHashMap<>();
// ==================== Spring Bean 静态注入 ==================== // ==================== Spring Bean 静态注入 ====================
/** /**
...@@ -734,6 +741,55 @@ public class CoWebSocketServer { ...@@ -734,6 +741,55 @@ public class CoWebSocketServer {
return; return;
} }
// ========== 投屏切换 ==========
if ("presenter_change".equals(action) || "presenter_invite".equals(action) ||
"presenter_response".equals(action) || "presenter_revoke".equals(action)) {
if ("presenter_invite".equals(action)) {
// 定向发送给目标用户类型
String targetUserType = json.has("target") ? json.get("target").asText() : null;
if (targetUserType != null) {
sendToUserType(roomId, targetUserType, message);
} else {
broadcast(roomId, message, session);
}
} else if ("presenter_response".equals(action)) {
boolean accept = json.has("accept") && json.get("accept").asBoolean();
if (accept) {
String target = json.has("target") ? json.get("target").asText() : null;
if (target != null) {
PRESENTER_MAP.put(roomId, target);
log.info("房间 {} 投屏者更新为: {}", roomId, target);
}
}
// 广播结果给所有人
broadcast(roomId, message, session);
} else {
// presenter_change 或 presenter_revoke 直接广播
broadcast(roomId, message, session);
}
return;
}
// ========== 远程控制 ==========
if ("request_remote_control".equals(action) || "remote_control_response".equals(action) ||
"remote_control_stop".equals(action)) {
if ("request_remote_control".equals(action)) {
// 向当前投屏者发送请求
String presenterType = PRESENTER_MAP.get(roomId);
if (presenterType != null) {
sendToUserType(roomId, presenterType, message);
} else {
log.warn("房间 {} 无人投屏,忽略远程控制请求", roomId);
}
} else {
// response 或 stop 广播给所有人
broadcast(roomId, message, session);
}
return;
}
// --- 未知操作:记录警告日志 --- // --- 未知操作:记录警告日志 ---
log.warn("未知 action: {}", action); log.warn("未知 action: {}", action);
...@@ -869,6 +925,7 @@ public class CoWebSocketServer { ...@@ -869,6 +925,7 @@ public class CoWebSocketServer {
} }
} }
} }
PRESENTER_MAP.remove(roomId);
// 2. 清理 Redis 中的成员信息 Hash // 2. 清理 Redis 中的成员信息 Hash
redisTemplate.delete(String.format(ROOM_MEMBERS_KEY, roomId)); redisTemplate.delete(String.format(ROOM_MEMBERS_KEY, roomId));
// 3. 清理 Redis 中的页面状态 // 3. 清理 Redis 中的页面状态
...@@ -959,4 +1016,22 @@ public class CoWebSocketServer { ...@@ -959,4 +1016,22 @@ public class CoWebSocketServer {
return "unknown"; return "unknown";
} }
/**
* 向房间内指定用户类型的所有用户发送消息(定向发送)
*/
private void sendToUserType(String roomId, String targetUserType, String message) {
Set<Session> sessions = ROOMS.get(roomId);
if (sessions == null || sessions.isEmpty()) return;
for (Session s : sessions) {
String userType = SESSION_USER_TYPE.get(s);
if (targetUserType.equals(userType) && s.isOpen()) {
try {
s.getBasicRemote().sendText(message);
} catch (IOException e) {
log.warn("发送消息给用户类型 {} 失败: {}", targetUserType, e.getMessage());
}
}
}
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment