Websockets 在 Magnolia CMS 上使用 Spring + SockJS + STOMP
Websockets Using Spring + SockJS + STOMP on Magnolia CMS
我正在尝试在 Tomcat 上为我的网络应用 运行 实现一些 Websocket 功能。我正在使用以下技术:
- (服务器)Spring Websocket + Spring 消息
- (客户端)SockJS + Stomp.js
我正在遵循这个指南(大致):http://g00glen00b.be/spring-angular-sockjs/
我的配置与指南类似,但我将在此处添加一些代码片段:
客户:
s.socket = new SockJS('http://localhost:8181/.cckiosk/socket/test');
//s.socket = new SockJS('http://localhost:8181/.cckiosk/socket/test', {}, { transports: ['xhr-polling'] });
s.client = Stomp.over(s.socket);
s.client.connect({}, onConnect);
s.client.onclose = onDisconnect;
服务器:
@Configuration
@ComponentScan
@EnableWebSocketMessageBroker
public class ModuleWebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry broker) {
//Prefix for messages FROM server TO client
broker.enableSimpleBroker("/client");
//Prefix for messages FROM client TO server
broker.setApplicationDestinationPrefixes("/server");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/socket/test").setAllowedOrigins("*").withSockJS();
}
}
控制器:
@MessageMapping("/socket/test")
@SendTo("/client/message")
public GenericMessage doSample(GenericMessage msg) {
log.info("doSample: " + JsonUtil.jsonify(msg));
return new GenericMessage(msg.getId(), msg.getMessage(), new Date());
}
我在香草 Spring 应用程序中设置了所有内容,并且一切正常。
但是,当我将相同的代码移植到 Magnolia 模块时,代码停止工作并且我可以看到以下错误:
客户端错误:
服务器端错误:
2015-09-19 16:28:43,412 DEBUG eb.socket.handler.LoggingWebSocketHandlerDecorator: New WebSocketServerSockJsSession[id=tde1syjd]
2015-09-19 16:28:43,413 DEBUG eb.socket.handler.LoggingWebSocketHandlerDecorator: Transport error in WebSocketServerSockJsSession[id=tde1syjd]
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:196)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at org.apache.coyote.http11.upgrade.BioServletInputStream.doRead(BioServletInputStream.java:37)
at org.apache.coyote.http11.upgrade.AbstractServletInputStream.read(AbstractServletInputStream.java:129)
at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:47)
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler$WsReadListener.onDataAvailable(WsHttpUpgradeHandler.java:203)
at org.apache.coyote.http11.upgrade.AbstractServletInputStream.onDataAvailable(AbstractServletInputStream.java:203)
at org.apache.coyote.http11.upgrade.AbstractProcessor.upgradeDispatch(AbstractProcessor.java:93)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
实际上是连接重置或管道损坏。
将传输协议更改为非 websocket 协议(例如 xhr-polling
)也无济于事。
知道 Magnolia 中的什么会导致长期存在的 Websocket 会话关闭其连接吗?
解决方案在于完全绕过 Magnolia 过滤器链。
我只需要在 Magnolia admincentral 中创建一个全局旁路对象:配置 > server/filters/bypasses
class: info.magnolia.StartsWithURIVoter
pattern: /socket
并且连接不会再重置或中断。
在 Mangolia 5 中是 'info.magnolia.voting.voters.URIStartsWithVoter',而不是 'info.magnolia.StartsWithURIVoter'
我正在尝试在 Tomcat 上为我的网络应用 运行 实现一些 Websocket 功能。我正在使用以下技术:
- (服务器)Spring Websocket + Spring 消息
- (客户端)SockJS + Stomp.js
我正在遵循这个指南(大致):http://g00glen00b.be/spring-angular-sockjs/
我的配置与指南类似,但我将在此处添加一些代码片段:
客户:
s.socket = new SockJS('http://localhost:8181/.cckiosk/socket/test');
//s.socket = new SockJS('http://localhost:8181/.cckiosk/socket/test', {}, { transports: ['xhr-polling'] });
s.client = Stomp.over(s.socket);
s.client.connect({}, onConnect);
s.client.onclose = onDisconnect;
服务器:
@Configuration
@ComponentScan
@EnableWebSocketMessageBroker
public class ModuleWebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry broker) {
//Prefix for messages FROM server TO client
broker.enableSimpleBroker("/client");
//Prefix for messages FROM client TO server
broker.setApplicationDestinationPrefixes("/server");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/socket/test").setAllowedOrigins("*").withSockJS();
}
}
控制器:
@MessageMapping("/socket/test")
@SendTo("/client/message")
public GenericMessage doSample(GenericMessage msg) {
log.info("doSample: " + JsonUtil.jsonify(msg));
return new GenericMessage(msg.getId(), msg.getMessage(), new Date());
}
我在香草 Spring 应用程序中设置了所有内容,并且一切正常。
但是,当我将相同的代码移植到 Magnolia 模块时,代码停止工作并且我可以看到以下错误:
客户端错误:
服务器端错误:
2015-09-19 16:28:43,412 DEBUG eb.socket.handler.LoggingWebSocketHandlerDecorator: New WebSocketServerSockJsSession[id=tde1syjd]
2015-09-19 16:28:43,413 DEBUG eb.socket.handler.LoggingWebSocketHandlerDecorator: Transport error in WebSocketServerSockJsSession[id=tde1syjd]
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:196)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at org.apache.coyote.http11.upgrade.BioServletInputStream.doRead(BioServletInputStream.java:37)
at org.apache.coyote.http11.upgrade.AbstractServletInputStream.read(AbstractServletInputStream.java:129)
at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:47)
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler$WsReadListener.onDataAvailable(WsHttpUpgradeHandler.java:203)
at org.apache.coyote.http11.upgrade.AbstractServletInputStream.onDataAvailable(AbstractServletInputStream.java:203)
at org.apache.coyote.http11.upgrade.AbstractProcessor.upgradeDispatch(AbstractProcessor.java:93)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
实际上是连接重置或管道损坏。
将传输协议更改为非 websocket 协议(例如 xhr-polling
)也无济于事。
知道 Magnolia 中的什么会导致长期存在的 Websocket 会话关闭其连接吗?
解决方案在于完全绕过 Magnolia 过滤器链。
我只需要在 Magnolia admincentral 中创建一个全局旁路对象:配置 > server/filters/bypasses
class: info.magnolia.StartsWithURIVoter
pattern: /socket
并且连接不会再重置或中断。
在 Mangolia 5 中是 'info.magnolia.voting.voters.URIStartsWithVoter',而不是 'info.magnolia.StartsWithURIVoter'