加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java使用websocket,并且获取HttpSession 源码分析(推荐)

发布时间:2020-05-25 19:23:52 所属栏目:Java 来源:互联网
导读:一:本文使用范围此文不仅仅局限于springboot,普通的spring工程,甚至是servlet工程,都是一样的,只不过配置一些监听器的方法不同而已。

一:本文使用范围

此文不仅仅局限于spring boot,普通的spring工程,甚至是servlet工程,都是一样的,只不过配置一些监听器的方法不同而已。

本文经过作者实践,确认完美运行。

二:Spring boot使用websocket

2.1:依赖包

websocket本身是servlet容器所提供的服务,所以需要在web容器中运行,像我们所使用的tomcat,当然,spring boot中已经内嵌了tomcat。

websocket遵循了javaee规范,所以需要引入javaee的包

<dependency>
   <groupId>javax</groupId>
   <artifactId>javaee-api</artifactId>
   <version>7.0</version>
   <scope>provided</scope>
  </dependency>

当然,其实tomcat中已经自带了这个包。

如果是在spring boot中,还需要加入websocket的starter

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-websocket</artifactId>
      <version>1.4.0.RELEASE</version>
    </dependency>

2.2:配置websocket

如果不是spring boot项目,那就不需要进行这样的配置,因为如果在tomcat中运行的话,tomcat会扫描带有@ServerEndpoint的注解成为websocket,而spring boot项目中需要由这个bean来提供注册管理。

@Configuration
public class WebSocketConfig {
  @Bean
  public ServerEndpointExporter serverEndpointExporter() {
    return new ServerEndpointExporter();
  }
}

2.3:websocket的java代码

使用websocket的核心,就是一系列的websocket注解,@ServerEndpoint是注册在类上面开启。

@ServerEndpoint(value = "/websocket")
@Component
public class MyWebSocket {

  //与某个客户端的连接会话,需要通过它来给客户端发送数据
  private Session session;

  /**
   * 连接成功*/
  @OnOpen
  public void onOpen(Session session) {
    this.session = session;
  }

  /**
   * 连接关闭调用的方法
   */
  @OnClose
  public void onClose() {
  }

  /**
   * 收到消息
   *
   * @param message 
  */
  @OnMessage
  public void onMessage(String message,Session session) {
    System.out.println("来自浏览器的消息:" + message);

    //群发消息
    for (MyWebSocket item : webSocketSet) {
      try {
        item.sendMessage(message);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  /**
   * 发生错误时调用
   */
  @OnError
  public void onError(Session session,Throwable error) {
    System.out.println("发生错误");
    error.printStackTrace();
  }
  public void sendMessage(String message) throws IOException {
    this.session.getBasicRemote().sendText(message);//同步
    //this.session.getAsyncRemote().sendText(message);//异步
  }
}

其实我也感觉很奇怪,为什么不使用接口来规范。即使是因为@ServerEndpoint注解中其它属性中可以定义出一些额外的参数,但相信也是可以抽象出来的,不过想必javaee这样做,应该是有它的用意吧。

2.4:浏览器端的代码

浏览器端的代码需要浏览器支持websocket,当然,也有socket.js可以支持到ie7,但是这个我没用过。毕竟ie基本上没人用的,市面上的浏览器基本上全部都支持websocket。

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
</body>
<script type="text/javascript">
  var websocket = null;
  //判断当前浏览器是否支持WebSocket
  if('WebSocket' in window){
    websocket = new WebSocket("ws://localhost:9999/websocket");
  }
  else{
    alert('不支持websocket')
  }
  //连接发生错误
  websocket.onerror = function(){
    
  };
  //连接成功
  websocket.onopen = function(event){  
  }
  //接收到消息
  websocket.onmessage = function(event){
    var msg = event.data;
    alert("收到消息:" + msg);
  }
  //连接关闭
  websocket.onclose = function(){    
  }
  //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
  window.onbeforeunload = function(){
    websocket.close();
  }
  //发送消息
  function send(message){
    websocket.send(message);
  }
</script>
</html>

 如此就连接成功了。

三:获取HttpSession,源码分析

获取HttpSession是一个很有必要讨论的问题,因为java后台需要知道当前是哪个用户,用以处理该用户的业务逻辑,或者是对该用户进行授权之类的,但是由于websocket的协议与Http协议是不同的,所以造成了无法直接拿到session。但是问题总是要解决的,不然这个websocket协议所用的场景也就没了。

3.1:获取HttpSession的工具类,源码详细分析

我们先来看一下@ServerEndpoint注解的源码

package javax.websocket.server;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.websocket.Decoder;
import javax.websocket.Encoder;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ServerEndpoint {
  /**
   * URI or URI-template that the annotated class should be mapped to.
   * @return The URI or URI-template that the annotated class should be mapped
   *     to.
   */
  String value();
  String[] subprotocols() default {};
  Class<? extends Decoder>[] decoders() default {};
  Class<? extends Encoder>[] encoders() default {};
  public Class<? extends ServerEndpointConfig.Configurator> configurator()
      default ServerEndpointConfig.Configurator.class;
}

我们看到最后的一个方法,也就是加粗的方法。可以看到,它要求返回一个ServerEndpointConfig.Configurator的子类,我们写一个类去继承它。

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import javax.websocket.server.ServerEndpointConfig.Configurator;
public class HttpSessionConfigurator extends Configurator {
  @Override
  public void modifyHandshake(ServerEndpointConfig sec,HandshakeRequest request,HandshakeResponse response) {
    //怎么搞?
  }
}

当我们覆盖modifyHandshake方法时,可以看到三个参数,其中后面两个参数让我们感觉有点见过的感觉,我们查看一HandshakeRequest的源码

package javax.websocket.server;
import java.net.URI;
import java.security.Principal;
import java.util.List;
import java.util.Map;
/**
 * Represents the HTTP request that asked to be upgraded to WebSocket.
 */
public interface HandshakeRequest {
  static final String SEC_WEBSOCKET_KEY = "Sec-WebSocket-Key";
  static final String SEC_WEBSOCKET_PROTOCOL = "Sec-WebSocket-Protocol";
  static final String SEC_WEBSOCKET_VERSION = "Sec-WebSocket-Version";
  static final String SEC_WEBSOCKET_EXTENSIONS= "Sec-WebSocket-Extensions";
  Map<String,List<String>> getHeaders();
  Principal getUserPrincipal();
  URI getRequestURI();
  boolean isUserInRole(String role);
  /**
   * Get the HTTP Session object associated with this request. Object is used
   * to avoid a direct dependency on the Servlet API.
   * @return The javax.servlet.http.HttpSession object associated with this
   *     request,if any.
   */
  Object getHttpSession();
  Map<String,List<String>> getParameterMap();
  String getQueryString();
}

我们发现它是一个接口,接口中规范了这样的一个方法

  /**
   * Get the HTTP Session object associated with this request. Object is used
   * to avoid a direct dependency on the Servlet API.
   * @return The javax.servlet.http.HttpSession object associated with this
   *     request,if any.
   */
  Object getHttpSession();

上面有相应的注释,说明可以从Servlet API中获取到相应的HttpSession。

当我们发现这个方法的时候,其实已经松了一口气了。

那么我们就可以补全未完成的代码

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import javax.websocket.server.ServerEndpointConfig.Configurator;

/**
 * 从websocket中获取用户session
 *
 *
 */
public class HttpSessionConfigurator extends Configurator {
  @Override
  public void modifyHandshake(ServerEndpointConfig sec,HandshakeResponse response) {
     HttpSession httpSession = (HttpSession) request.getHttpSession();
          sec.getUserProperties().put(HttpSession.class.getName(),httpSession);
  }
}

其实通过上面的源码分析,你们应该知道了HttpSession的获取。但是下面又多了一行代码

 sec.getUserProperties().put(HttpSession.class.getName(),httpSession);

这行代码又是什么意思呢?

我们看一下ServerEnpointConfig的声明

public interface ServerEndpointConfig extends EndpointConfig

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读