fix: 优化网页端不能连接的问题

This commit is contained in:
2026-07-17 21:14:45 +08:00
parent b8edd5eff5
commit 0cb6b75a54
5 changed files with 110 additions and 22 deletions

View File

@@ -1,10 +1,12 @@
package com.ttstd.signaling.config;
import com.ttstd.signaling.handler.SignalWebSocketHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
@Configuration
@EnableWebSocket
@@ -16,6 +18,21 @@ public class WebSocketConfig implements WebSocketConfigurer {
this.signalWebSocketHandler = signalWebSocketHandler;
}
/**
* 配置 WebSocket 容器的消息大小限制与会话超时。
* <p>默认文本消息缓冲区为 8KBWebRTC SDPOFFER/ANSWER消息经 JSON 包装后
* 可能超过该限制,导致服务端抛出 TextMessageLimitException 并关闭连接。
* 此处将文本消息上限设为 512KB二进制消息上限设为 512KB会话空闲超时设为 10 分钟。
*/
@Bean
public ServletServerContainerFactoryBean createWebSocketContainer() {
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
container.setMaxTextMessageBufferSize(512 * 1024); // 512 KB
container.setMaxBinaryMessageBufferSize(512 * 1024); // 512 KB
container.setMaxSessionIdleTimeout(600_000L); // 10 分钟
return container;
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(signalWebSocketHandler, "/ws/signal")

View File

@@ -71,6 +71,9 @@ public class SignalWebSocketHandler extends TextWebSocketHandler {
case "DEVICE_LIST":
handleDeviceList(session, signalMessage);
break;
case "PING":
// 客户端心跳保活消息,无需处理,仅用于防止中间代理因空闲超时断开连接
break;
case "OFFER":
// 连接请求:统一经 ConnectionRequestManager 做校验/去重/待确认跟踪后再转发
handleConnectionRequest(signalMessage);