feat: 目标不在线时进行提示和更新UI
This commit is contained in:
@@ -57,13 +57,17 @@ public class SignalWebSocketHandler extends TextWebSocketHandler {
|
||||
break;
|
||||
case "OFFER":
|
||||
case "ANSWER":
|
||||
// 关键信令:目标不在线时回送 TARGET_OFFLINE 提醒发送方
|
||||
forwardMessage(signalMessage, true);
|
||||
break;
|
||||
case "ICE_CANDIDATE":
|
||||
case "CONTROL_COMMAND":
|
||||
forwardMessage(signalMessage);
|
||||
// 普通信令:目标不在线时静默丢弃,不返回提示
|
||||
forwardMessage(signalMessage, false);
|
||||
break;
|
||||
default:
|
||||
// 其他消息类型直接转发
|
||||
forwardMessage(signalMessage);
|
||||
// 其他消息类型直接转发,目标不在线时静默丢弃
|
||||
forwardMessage(signalMessage, false);
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -122,7 +126,7 @@ public class SignalWebSocketHandler extends TextWebSocketHandler {
|
||||
sendToSession(session, response);
|
||||
}
|
||||
|
||||
private void forwardMessage(SignalMessage message) {
|
||||
private void forwardMessage(SignalMessage message, boolean notifyOffline) {
|
||||
String toDeviceId = message.getToDeviceId();
|
||||
if (toDeviceId == null) {
|
||||
logger.warn("Cannot forward message: toDeviceId is null");
|
||||
@@ -132,6 +136,10 @@ public class SignalWebSocketHandler extends TextWebSocketHandler {
|
||||
WebSocketSession targetSession = sessionManager.getSession(toDeviceId);
|
||||
if (targetSession == null || !targetSession.isOpen()) {
|
||||
logger.warn("Target device {} is not online", toDeviceId);
|
||||
// 仅 OFFER / ANSWER 在目标不在线时回送 TARGET_OFFLINE,其余类型静默丢弃
|
||||
if (notifyOffline) {
|
||||
notifySenderTargetOffline(message, toDeviceId);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -144,6 +152,31 @@ public class SignalWebSocketHandler extends TextWebSocketHandler {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 目标被控端不在线时,向发送方(主控端)回送 TARGET_OFFLINE 提醒。
|
||||
*/
|
||||
private void notifySenderTargetOffline(SignalMessage message, String offlineDeviceId) {
|
||||
String fromDeviceId = message.getFromDeviceId();
|
||||
if (fromDeviceId == null) {
|
||||
logger.warn("Cannot notify offline state: fromDeviceId is null");
|
||||
return;
|
||||
}
|
||||
|
||||
WebSocketSession senderSession = sessionManager.getSession(fromDeviceId);
|
||||
if (senderSession == null || !senderSession.isOpen()) {
|
||||
logger.warn("Sender {} session not found, cannot notify target offline", fromDeviceId);
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("type", "TARGET_OFFLINE");
|
||||
response.put("toDeviceId", offlineDeviceId);
|
||||
response.put("payload", "目标被控端(" + offlineDeviceId + ")不在线,请确认设备已开启并连接到信令服务器");
|
||||
|
||||
sendToSession(senderSession, response);
|
||||
logger.info("Notified sender {} that target {} is offline", fromDeviceId, offlineDeviceId);
|
||||
}
|
||||
|
||||
private void sendToSession(WebSocketSession session, Object data) {
|
||||
try {
|
||||
String json = objectMapper.writeValueAsString(data);
|
||||
|
||||
Reference in New Issue
Block a user