项目

一般

简介

aas敏捷版 WebSocket使用问题总结

佘 肃徽大约 3 年 之前添加

aas10敏捷版自身是支持websocket协议的,在使用标准socket协议的时候,加入依赖就可以了。

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
   <groupId>com.apusic</groupId>
   <artifactId>aams-spring-boot-websocket-starter</artifactId>
   <version>2.1.7.RELEASE</version>
</dependency>

但是,如果要使用WebSocket下的子协议STOMP协议时就要注意了,如果是war包部署,运行时会报异常。

java.lang.IllegalStateException: No suitable default RequestUpgradeStrategy found
        at org.springframework.web.socket.server.support.AbstractHandshakeHandler.initRequestUpgradeStrategy(AbstractHandshakeHandler.java:143) ~[spring-websocket-4.3.29.RELEASE.jar:4.3.29.RELEASE]
        at org.springframework.web.socket.server.support.AbstractHandshakeHandler.<init>(AbstractHandshakeHandler.java:109) ~[spring-websocket-4.3.29.RELEASE.jar:4.3.29.RELEASE]
        at org.springframework.web.socket.server.support.DefaultHandshakeHandler.<init>(DefaultHandshakeHandler.java:35) ~[spring-websocket-4.3.29.RELEASE.jar:4.3.29.RELEASE]
        at org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService.getDefaultTransportHandlers(DefaultSockJsService.java:92) ~[spring-websocket-4.3.29.RELEASE.jar:4.3.29.RELEASE]
        at org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService.<init>(DefaultSockJsService.java:77) ~[spring-websocket-4.3.29.RELEASE.jar:4.3.29.RELEASE]
        at org.springframework.web.socket.config.annotation.SockJsServiceRegistration.createSockJsService(SockJsServiceRegistration.java:285) ~[spring-websocket-4.3.29.RELEASE.jar:4.3.29.RELEASE]
        at org.springframework.web.socket.config.annotation.SockJsServiceRegistration.getSockJsService(SockJsServiceRegistration.java:244) ~[spring-websocket-4.3.29.RELEASE.jar:4.3.29.RELEASE]
        at org.springframework.web.socket.config.annotation.WebMvcStompWebSocketEndpointRegistration.getMappings(WebMvcStompWebSocketEndpointRegistration.java:124) ~[spring-websocket-4.3.29.RELEASE.jar:4.3.29.RELEASE]
        at org.springframework.web.socket.config.annotation.WebMvcStompEndpointRegistry.getHandlerMapping(WebMvcStompEndpointRegistry.java:155) ~[spring-websocket-4.3.29.RELEASE.jar:4.3.29.RELEASE]
        at org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurationSupport.stompWebSocketHandlerMapping(WebSocketMessageBrokerConfigurationSupport.java:79) ~[spring-websocket-4.3.29.RELEASE.jar:4.3.29.RELEASE]
        at org.springframework.web.socket.config.annotation.DelegatingWebSocketMessageBrokerConfiguration$$EnhancerBySpringCGLIB$$492abd94.CGLIB$stompWebSocketHandlerMapping$9(<generated>) ~[spring-websocket-4.3.29.RELEASE.jar:4.3.29.RELEASE]
        at org.springframework.web.socket.config.annotation.DelegatingWebSocketMessageBrokerConfiguration$$EnhancerBySpringCGLIB$$492abd94$$FastClassBySpringCGLIB$$dffedcdc.invoke(<generated>) ~[spring-websocket-4.3.29.RELEASE.jar:4.3.29.RELEASE]
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.29.RELEASE.jar:4.3.29.RELEASE]
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.29.RELEASE.jar:4.3.29.RELEASE]
        at org.springframework.web.socket.config.annotation.DelegatingWebSocketMessageBrokerConfiguration$$EnhancerBySpringCGLIB$$492abd94.stompWebSocketHandlerMapping(<generated>) ~[spring-websocket-4.3.29.RELEASE.jar:4.3.29.RELEASE]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_302]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_302]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_302]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_302]
        at org.springframework.beans.factory.support.SimpleInstantiationS

 

原因为spring并未对aas做默认的协议支持,导致无法自动装配相关协议的配置。需要手动进行相关配置。如下述代码:

    public void registerStompEndpoints(StompEndpointRegistry registry) {

        TransportHandler[] handers=new TransportHandler[]{new WebSocketTransportHandler(
                new DefaultHandshakeHandler(new AasRequestUpgradeStrategy()))};

        //注册一个 Stomp 的节点(endpoint),并指定使用 SockJS 协议。
//        registry.addEndpoint("/endpointNasus").withSockJS();
        AasRequestUpgradeStrategy upgradeStrategy = new AasRequestUpgradeStrategy();
        registry.addEndpoint("/endpointNasus")
                .setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy))
                .setAllowedOrigins("*")
                .withSockJS()
                .setTransportHandlers(handers)
                      .setTransportHandlerOverrides();
    }

AasRequestUpgradeStrategy 类拷贝自 aams-spring-boot-websocket-starter-2.1.7.RELEASE,

具体方法可以参考 https://gitee.com/sherwinshe/aas-demo.git 里的 springboot-demo2