tongweb deploys the war package of springboot and performs compatible processing on spring-stomp

Posted May 28, 20203 min read

1. Introducing tongweb's websocket package

  1. There is a websocket related package websocket-jdk7orLater.jar under the tongweb/lib path, copy it out;

  2. Create a new lib directory under the project root directory and put the jar package just now;

  3. Maven increases dependency:

         <dependency>
             <groupId> com.tongweb.websocket </groupId>
             <artifactId> websocket-jdk7orLater </artifactId>
             <version> 1.0 </version>
             <systemPath> ${basedir} \ lib \ websocket-jdk7orLater.jar </systemPath>
             <scope> system </scope>
         </dependency>
  4. Add tongweb handshake strategy


  1. Create a new java class, copy the code in org.springframework.web.socket.server.standard.TomcatRequestUpgradeStrategy and create a new class TongwebUpgradeStrategy.java, and introduce the package:

    import com.tongweb.web.websocket.server.WsServerContainer;

    import java.io.IOException;
    import java.util.Collections;
    import java.util.List;
    import java.util.Map;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.websocket.Endpoint;
    import javax.websocket.Extension;

    import com.tongweb.web.websocket.server.WsServerContainer;
    import org.springframework.http.server.ServerHttpRequest;
    import org.springframework.http.server.ServerHttpResponse;
    import org.springframework.lang.Nullable;
    import org.springframework.web.socket.server.HandshakeFailureException;
    import org.springframework.web.socket.server.standard.AbstractStandardUpgradeStrategy;
    import org.springframework.web.socket.server.standard.ServerEndpointRegistration;

    public class TongwebUpgradeStrategy extends AbstractStandardUpgradeStrategy {

     public TongwebUpgradeStrategy() {
     }
    
     public String []getSupportedVersions() {
         return new String []{"13"};
     }
    
     public void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List <Extension> selectedExtensions, Endpoint endpoint) throws HandshakeFailureException {
         HttpServletRequest servletRequest = this.getHttpServletRequest(request);
         HttpServletResponse servletResponse = this.getHttpServletResponse(response);
         StringBuffer requestUrl = servletRequest.getRequestURL();
         String path = servletRequest.getRequestURI();
         Map <String, String> pathParams = Collections.emptyMap();
         ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(path, endpoint);
         endpointConfig.setSubprotocols(Collections.singletonList(selectedProtocol));
         endpointConfig.setExtensions(selectedExtensions);
    
         try {
             this.getContainer(servletRequest) .doUpgrade(servletRequest, servletResponse, endpointConfig, pathParams);
         } catch(ServletException var13) {
             throw new HandshakeFailureException("Servlet request failed to upgrade to WebSocket:" + requestUrl, var13);
         } catch(IOException var14) {
             throw new HandshakeFailureException("Response update failed during upgrade to WebSocket:" + requestUrl, var14);
         }
     }
    
     public WsServerContainer getContainer(HttpServletRequest request) {
         return(WsServerContainer) super.getContainer(request);
     }

    }

  2. Configure stomp


 @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        TongwebUpgradeStrategy upgradeStrategy = new TongwebUpgradeStrategy();
        registry.addEndpoint("/ws /")
                .setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy))
                .setAllowedOrigins("*")
                .withSockJS()
                .setTransportHandlers(getHandler(). toArray(new WebSocketTransportHandler(new DefaultHandshakeHandler(new TongwebUpgradeStrategy())))
                .setTransportHandlerOverrides();
    }

Deploying services, tongweb will not report that it cannot find an upgrade strategy