Gri*_*ris 6 spring spring-websocket
我必须在Websockets上实现自定义API,这需要:
所以我有以下问题:
将路径参数传递给消息处理程序的正确方法是什么?我可以在处理程序注册中使用ant模式
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(customHandler(), "/api/custom/{clientId}");
}
Run Code Online (Sandbox Code Playgroud)
但是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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后在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);
}
}
Run Code Online (Sandbox Code Playgroud)
但在我看来,这种方法有点笨拙.有没有更合适的方法来解决这个问题?
谢谢.
我能给出的最好建议是遵循内置的子协议支持的示例——从SubProtocolWebSocketHandler
和开始SubProtocolHandler
,它委托包括实现StompSubProtocolHandler
。进一步SubProtocolWebSocketHandler
连接到“clientInbound”和“clientOutbound”通道,然后使用这些通道形成处理流并提供线程边界。
有一个 STOMP 处理流程的描述http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-stomp-message-flow其中包括将消息委托给带注释的控制器和/或消息代理,消息代理也可以将消息发送回下游客户端。
本质上是与具有协议特定内容的 SpringStompSubProtocolHandler
之间的转换。因此,控制器、消息代理或来自客户端入站通道的消息的任何其他使用者都与 WebSocket 传输层分离并且不知道。许多围绕此类子协议消息的构建、发送和处理而构建的设施可能可用于支持其他类似 STOMP 的协议。这包括包中的所有类。WebSocketMessage
Message
org.springframework.messaging.simp
至于 URL 路径参数,Spring 在 WebSocket 级别没有提供任何内容,主要是传输层。大多数有趣的事情发生在子协议级别。例如,对于 STOMP,aMessageMapping
是基于目标标头支持的,与在 Spring MVC 中@DestinationVariable
使用的 a 相当@PathVariable
,但基于目标标头,而不是 URL。
归档时间: |
|
查看次数: |
688 次 |
最近记录: |