我必须通过 Websockets 实现自定义 API,这需要:
所以我有以下问题:
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(customHandler(), "/api/custom/{clientId}");
}
但这些似乎在 TextWebSocketHandler 上不可用。我现在通过以下方式扩展默认的 HttpSessionHandshakeInterceptor 解决了这个问题:
public class CustomHandshakeInterceptor extends HttpSessionHandshakeInterceptor {
private static final UriTemplate URI_TEMPLATE = new UriTemplate("/api/custom/{clientId}");
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
Map<String, String> segments = URI_TEMPLATE.match(request.getURI().getPath());
attributes.put("CLIENTID", segments.get("clientId"));
return super.beforeHandshake(request, response, wsHandler, attributes);
}
}
然后在 TextWebSocketHandler 中访问它:
public class CustomHandler extends TextWebSocketHandler {
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
super.handleTextMessage(session, message);
String clientId = session.getAttributes().get("CLIENTID");
...
session.sendMessage(response);
}
}
但在我看来,这种方法有点笨拙。有没有更合适的方法来解决这个问题?
谢谢。
最佳答案
我能给出的最好建议是遵循内置的子协议(protocol)支持示例——从 SubProtocolWebSocketHandler
开始。和 SubProtocolHandler
是它委托(delegate)包含StompSubProtocolHandler
执行。 SubProtocolWebSocketHandler
进一步连接到“clientInbound”和“clientOutbound” channel ,然后用于形成处理流以及提供线程边界。
STOMP的处理流程有描述http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-stomp-message-flow这包括将消息委托(delegate)给带注释的 Controller 和/或消息代理,后者也可以将消息发送回下游给客户端。
本质上是 StompSubProtocolHandler
转换为 WebSocketMessage
和一个 Spring Message
具有特定于协议(protocol)的内容。因此,来自客户端入站 channel 的消息的 Controller 、消息代理或任何其他消费者都与 WebSocket 传输层分离并且不知道。围绕此类子协议(protocol)消息的构建、发送和处理而构建的许多设施旨在潜在地用于支持其他类似 STOMP 的协议(protocol)。这包括 org.springframework.messaging.simp
中的所有类包裹。
至于 URL 路径参数,Spring 没有在 WebSocket 级别提供任何东西,这主要是一个传输层。大多数有趣的事情发生在子协议(protocol)级别。例如对于 STOMP 一个 MessageMapping
支持基于目标 header 以及 @DestinationVariable
这相当于使用 @PathVariable
在 Spring MVC 中,但基于目标 header ,而不是 URL。
关于spring - 在 Spring Websockets 上构建自定义 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35468770/