我必须在Spring引导应用程序中添加对自定义WebSocket子协议的支持(所以不是stomp),但是我很难理解我需要提供什么以及Spring已经提供了什么。
这就是我的目标:
@Configuration
@EnableWebSocket
public class WebSocketAutoConfiguration implements WebSocketConfigurer {
public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
webSocketHandlerRegistry.addHandler(this.webSocketHandler(), new String[]{endpointUrl});
}
@Bean
public WebSocketHandler webSocketHandler() {
ExecutorSubscribableChannel clientInboundChannel = new ExecutorSubscribableChannel();
ExecutorSubscribableChannel clientOutboundChannel = new ExecutorSubscribableChannel();
SubProtocolWebSocketHandler subProtocolWebSocketHandler = new SubProtocolWebSocketHandler(clientInboundChannel, clientOutboundChannel);
subProtocolWebSocketHandler.addProtocolHandler(new SubProtocolHandler() {
public List<String> getSupportedProtocols() {
return Collections.singletonList("custom-protocol");
}
public void handleMessageFromClient(WebSocketSession session, WebSocketMessage<?> message, MessageChannel outputChannel) throws Exception {
session.sendMessage(new TextMessage("some message"));
}
public void handleMessageToClient(WebSocketSession session, Message<?> message) throws Exception {
}
public String resolveSessionId(Message<?> message) {
return UUID.randomUUID().toString();
}
public void afterSessionStarted(WebSocketSession session, MessageChannel outputChannel) throws Exception {
System.out.println("SESSION STARTED");
}
public void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus, MessageChannel outputChannel) throws Exception {
session.close();
System.out.println("SESSION ENDED");
}
});
return subProtocolWebSocketHandler;
}
}
这是可行的,从这个意义上说
handleMessageFromClient
是否会触发Web套接字消息,但我无法理解
MessageChannel outputChannel
和
handleMessageToClient
.
有可能得到
PerConnectionWebSocketHandler
语义与
SubProtocolWebSocketHandler
?
围绕这一点的文档基本上是不存在的,例如
手持信息客户端
说:
将给定的@link消息处理到与给定WebSocket会话关联的客户机。
好吧,太棒了。Stomp实现令人难以置信,因此它们不能很好地用作指导方针。
任何例子,宽广的步骤或任何事情,真的,都会受到赞赏。