feat(gateway): support assign hostname on websocket gateway

This commit is contained in:
2026-04-20 14:38:58 +08:00
parent 95a3f782e6
commit 9108d1db46
2 changed files with 6 additions and 3 deletions

View File

@@ -34,11 +34,13 @@ public class WebSocketGateway extends WebSocketServer implements AgentGateway<In
// 记录最后一次收到Pong的时间 // 记录最后一次收到Pong的时间
private final ConcurrentHashMap<WebSocket, Long> lastPongTimes = new ConcurrentHashMap<>(); private final ConcurrentHashMap<WebSocket, Long> lastPongTimes = new ConcurrentHashMap<>();
public WebSocketGateway(int port, long heartbeatInterval) { public WebSocketGateway(int port, @NotNull String hostname, long heartbeatInterval) {
super(new InetSocketAddress(port)); super(new InetSocketAddress(hostname, port));
this.heartbeatInterval = heartbeatInterval; this.heartbeatInterval = heartbeatInterval;
this.setReuseAddr(true); this.setReuseAddr(true);
this.executor = Executors.newSingleThreadExecutor(); this.executor = Executors.newSingleThreadExecutor();
log.info("WebSocketGateway started on {}: {}", hostname, port);
} }
public void launch() { public void launch() {

View File

@@ -10,9 +10,10 @@ object WebSocketGatewayRegistration : AgentGatewayRegistration {
override fun create(params: Map<String, String>): AgentGateway<*, *> { override fun create(params: Map<String, String>): AgentGateway<*, *> {
val port = params["port"]?.toIntOrNull() ?: 29600 val port = params["port"]?.toIntOrNull() ?: 29600
val heartbeatInterval = params["heartbeat_interval"]?.toLongOrNull() ?: 10_000L val heartbeatInterval = params["heartbeat_interval"]?.toLongOrNull() ?: 10_000L
val hostname = params["hostname"] ?: "127.0.0.1"
require(port > 0) { "port must be greater than 0" } require(port > 0) { "port must be greater than 0" }
require(heartbeatInterval > 0) { "heartbeat_interval must be greater than 0" } require(heartbeatInterval > 0) { "heartbeat_interval must be greater than 0" }
return WebSocketGateway(port, heartbeatInterval) return WebSocketGateway(port, hostname, heartbeatInterval)
} }
override fun shutdown(instance: AgentGateway<*, *>) { override fun shutdown(instance: AgentGateway<*, *>) {