基于spring websocket+sockjs实现的长连接请求

1、前言

页面端通常有需求想要准实时知道后台数据的一个变化情况,比如扫码登录场景,或者跳转到网银支付场景,在旧有的短轮训实现下,通常造成大量的不必要请求和查询,这里基于spring websocket+sockjs来解决该问题。

2、websocket

websocket是html5的一个新特性,目前oracle已经统一java websocket api,只要容器支持JSR356(tomcat7以上支持),且jdk使用的是1.7以上,servlet-api3.1,即可使用websocket api提供服务。
在tomcat7之前也提供了tomcat专有的WebsocketServlet,不过在tomcat7已经deprecated,将在tomcat8中移除。

2.1使用jsr356标准的接口实现websocket

  1. tomcat提供了websocket调用示例,可参考server codehtml code
  2. 依赖

    <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-api</artifactId>
            <version>1.1</version>
            <scope>provided</scope>
        </dependency>
    

    注意websocket的scope需要是provided,在运行时使用tomcat提供的jar运行,不然建立连接时会404。

  3. js实现websocket调用

    建立连接和注册事件

    
    var target = "ws://127.0.0.1/websocket/testWsAnnotation";
    if ('WebSocket' in window) {
        ws = new WebSocket(target);
    } else if ('MozWebSocket' in window) {
        ws = new MozWebSocket(target);
    } else {
        alert('WebSocket is not supported by this browser.');
        return;
    }
    ws.onopen = function () {
        log('Info: WebSocket connection opened.');
    };
    ws.onmessage = function (event) {
        log('Received: ' + event.data);
    };
    ws.onclose = function () {
        log('Info: WebSocket connection closed.');
    };
    

    关闭连接

    
    if (ws != null) {
        ws.close();
        ws = null;
    }
    

    发送消息

    
    if (ws != null) {
        var message = document.getElementById('message').value;
        log('Sent: ' + message);
        ws.send(message);
    } else {
        alert('WebSocket connection not established, please connect.');
    }
    

    需要注意的是,js建立连接处完整的js代码要执行完成退出后才会真正发起建立连接请求,如果在此之前发送消息则会报错如下:

    InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable
    

    如果是sockjs则会报错如下

    Error: InvalidStateError: The connection has not been established yet
    
  4. 服务端实现

    • 需要增加一个配置类,tomcat启动时会将扫描到的配置注解或者实现Endpoint的类传入该配置类方法,再由该配置类中的方法过滤哪些可做为暴露websocket服务的

      public class WebSocketConfig implements ServerApplicationConfig
      {
      
          @Override
          public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> scanned)
          {
              Set<ServerEndpointConfig> result = new HashSet<ServerEndpointConfig>();
              for (Class<? extends Endpoint> ep : scanned)
              {
                  result.add(ServerEndpointConfig.Builder.create(ep, "/websocket/" + WordUtils.uncapitalize(ep.getSimpleName())).build());
              }
              return result;
          }
      
          @Override
          public Set<Class<?>> getAnnotatedEndpointClasse
  • 8
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要在Zuul中使用WebSocketSockJS,您需要进行以下配置: 1. 添加依赖项 ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul-websocket</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> ``` 2. 配置Zuul路由 ```yml zuul: routes: websocket: path: /websocket/** url: ws://localhost:8081 ``` 这将把所有以“/websocket”开头的请求路由到WebSocket服务器上。 3. 配置SockJS ```java @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic"); config.setApplicationDestinationPrefixes("/app"); } @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/websocket").setAllowedOrigins("*").withSockJS(); } } ``` 这将配置一个SockJS端点,它将处理所有以“/websocket”开头的请求,并使用简单的代理模式将消息转发到“/topic”目的地。 4. 启用Zuul ```java @SpringBootApplication @EnableZuulProxy public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 这将启用Zuul代理,并将它们路由到相应的WebSocket服务器和SockJS端点。 现在,您应该可以在Zuul中使用WebSocketSockJS了。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值