Web容器在Magnolia CMS上使用Spring + SockJS + STOMP

时间:2015-09-19 08:36:15

标签: java websocket spring-websocket sockjs magnolia

我正在尝试为在Tomcat 7上运行的网络应用实施一些Websocket功能。我使用以下技术:

  • (服务器)Spring Websocket + Spring Messaging
  • (客户端)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模块时,代码停止工作,我可以看到以下错误:

客户端错误:

Client-side error

服务器端错误:

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会话关闭它的连接吗?

2 个答案:

答案 0 :(得分:2)

解决方案就是完全绕过Magnolia过滤链。

我只需要在Magnolia admincentral中创建一个全局绕过对象:Configuration>服务器/滤波器/旁路

class: info.magnolia.StartsWithURIVoter
pattern: /socket

连接不再重置或中断。

答案 1 :(得分:0)

在Mangolia 5中,它是' info.magnolia.voting.voters.URIStartsWithVoter',而不是' info.magnolia.StartsWithURIVoter'