成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

SpringBoot使用WebSocket實現即時消息

開發(fā) 前端
當以jar包形式運行時需要配置該bean,暴露我們配置的@ServerEndpoint;當我們以war獨立tomcat運行時不能配置該bean。這里有個g-messages.js文件是我寫的一個工具類,用來做連接及心跳檢查用的。

環(huán)境:SpringBoot2.4.12.


依賴

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

定義消息類型

  • 抽象消息對象
public class AbstractMessage {
  /**
   *   消息類型
   */
  protected String type ;
  
  /**
   *   消息內容
   */
  protected String content ;
  /**
   *   消息日期
   */
  protected String date ;
}

消息對象子類

1、Ping檢查消息

public class PingMessage extends AbstractMessage {
  public PingMessage() {}
  public PingMessage(String type) {
    this.type = type ;
  }
}

2、系統(tǒng)消息

public class SystemMessage extends AbstractMessage {
  public SystemMessage() {}
  public SystemMessage(String type, String content) {
    this.type = type ;
    this.content = content ;
  }
}

3、點對點消息

public class PersonMessage extends AbstractMessage {
  private String fromName ;
  private String toName ;
}

消息類型定義

public enum MessageType {
  
  /**
   *   系統(tǒng)消息 0000;心跳檢查消息 0001;點對點消息2001
   */
  SYSTEM("0000"), PING("0001"), PERSON("2001") ;
  
  private String type ;
  
  private MessageType(String type) {
    this.type = type ;
  }


  public String getType() {
    return type;
  }


  public void setType(String type) {
    this.type = type;
  }
  
}

WebSocket服務端點

該類作用就是定義客戶端連接的地址

@ServerEndpoint(value = "/message/{username}", 
  encoders = {WsMessageEncoder.class},
  decoders = {WsMessageDecoder.class},
  subprotocols = {"gmsg"},
  configurator = MessageConfigurator.class)  
@Component  
public class GMessageListener {  
  
    public static ConcurrentMap<String, UserSession> sessions = new ConcurrentHashMap<>();
    private static Logger logger = LoggerFactory.getLogger(GMessageListener.class) ;
  
    private String username ;
    
    @OnOpen  
    public void onOpen(Session session, EndpointConfig config, @PathParam("username") String username){
      UserSession userSession = new UserSession(session.getId(), username, session) ;
      this.username = username ;
      sessions.put(username, userSession) ;
      logger.info("【{}】用戶進入, 當前連接數:{}", username, sessions.size()) ; 
    }  
  
    @OnClose  
    public void onClose(Session session, CloseReason reason){  
      UserSession userSession = sessions.remove(this.username) ;
      if (userSession != null) {
        logger.info("用戶【{}】, 斷開連接, 當前連接數:{}", username, sessions.size()) ;
      }
    }
    
    @OnMessage
    public void pongMessage(Session session, PongMessage message) {
      ByteBuffer buffer = message.getApplicationData() ;
      logger.debug("接受到Pong幀【這是由瀏覽器發(fā)送】:" + buffer.toString());
    }
    
    @OnMessage
    public void onMessage(Session session, AbstractMessage message) {
      if (message instanceof PingMessage) {
        logger.debug("這里是ping消息");
        return ;
      }
      if (message instanceof PersonMessage) {
        PersonMessage personMessage = (PersonMessage) message ;
        if (this.username.equals(personMessage.getToName())) {
          logger.info("【{}】收到消息:{}", this.username, personMessage.getContent());
        } else {
          UserSession userSession = sessions.get(personMessage.getToName()) ;
          if (userSession != null) {
            try {
            userSession.getSession().getAsyncRemote().sendText(new ObjectMapper().writeValueAsString(message)) ;
          } catch (JsonProcessingException e) {
            e.printStackTrace();
          }
          }
        }
        return ;
      }
      if (message instanceof SystemMessage) {
        logger.info("接受到消息類型為【系統(tǒng)消息】") ; 
        return ;
      }
    }
    
    @OnError
    public void onError(Session session, Throwable error) {
      logger.error(error.getMessage()) ;
    }
}

WsMessageEncoder.java類
該類的主要作用是,當發(fā)送的消息是對象時,該如何轉換

public class WsMessageEncoder implements Encoder.Text<AbstractMessage> {
  private static Logger logger = LoggerFactory.getLogger(WsMessageDecoder.class) ;
  @Override
  public void init(EndpointConfig endpointConfig) {
  }
  @Override
  public void destroy() {
  }
  @Override
  public String encode(AbstractMessage tm) throws EncodeException {
    String message = null ;
    try {
      message = new ObjectMapper().writeValueAsString(tm);
    } catch (JsonProcessingException e) {
      logger.error("JSON處理錯誤:{}", e) ;
    }
    return message;
  }
}

WsMessageDecoder.java類
該類的作用是,當接收到消息時如何轉換成對象。

public class WsMessageDecoder implements  Decoder.Text<AbstractMessage> {


  private static Logger logger = LoggerFactory.getLogger(WsMessageDecoder.class) ;
  private static Set<String> msgTypes = new HashSet<>() ;
  
  static {
    msgTypes.add(MessageType.PING.getType()) ;
    msgTypes.add(MessageType.SYSTEM.getType()) ;
    msgTypes.add(MessageType.PERSON.getType()) ;
  }
  @Override
  @SuppressWarnings("unchecked")
  public AbstractMessage decode(String s) throws DecodeException {
    AbstractMessage message = null ;
    try {
      ObjectMapper mapper = new ObjectMapper() ;
      Map<String,String> map = mapper.readValue(s, Map.class) ;
      String type = map.get("type") ;
      switch(type) {
        case "0000":
          message = mapper.readValue(s, SystemMessage.class) ;
          break;
        case "0001":
          message = mapper.readValue(s, PingMessage.class) ;
          break;
        case "2001":
          message = mapper.readValue(s, PersonMessage.class) ;
          break;
      }
    } catch (JsonProcessingException e) {
      logger.error("JSON處理錯誤:{}", e) ;
    }
    return message ;
  }


  // 該方法判斷消息是否可以被解碼(轉換)
  @Override
  @SuppressWarnings("unchecked")
  public boolean willDecode(String s) {
    Map<String, String> map = new HashMap<>() ;
    try {
      map = new ObjectMapper().readValue(s, Map.class);
    } catch (JsonProcessingException e) {
      e.printStackTrace();
    }
    logger.debug("檢查消息:【" + s + "】是否可以解碼") ;
    String type = map.get("type") ;
    if (StringUtils.isEmpty(type) || !msgTypes.contains(type)) {
      return false ;
    }
    return true ;
  }
  @Override
  public void init(EndpointConfig endpointConfig) {
  }
  @Override
  public void destroy() {
  }
}

MessageConfigurator.java類
該類的作用是配置服務端點,比如配置握手信息

public class MessageConfigurator extends ServerEndpointConfig.Configurator {
  private static Logger logger = LoggerFactory.getLogger(MessageConfigurator.class) ;
  @Override
  public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
    logger.debug("握手請求頭信息:" + request.getHeaders());
    logger.debug("握手響應頭信息:" + response.getHeaders());
    super.modifyHandshake(sec, request, response);
  }  
}

WebSocke配置類

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

當以jar包形式運行時需要配置該bean,暴露我們配置的@ServerEndpoint;當我們以war獨立tomcat運行時不能配置該bean。

前端頁面

<!doctype html>
<html>
 <head>
  <meta charset="UTF-8">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <script src="g-messages.js?v=1"></script>
  <title>WebSocket</title>
  <style type="text/css">
</style>
  <script>
  let gm = null ;
  let username = null ;
  function ListenerMsg({url, protocols = ['gmsg'], options = {}}) {
    if (!url){ 
      throw new Error("未知服務地址") ;
    }
    gm = new window.__GM({
      url: url,
      protocols: protocols
    }) ;
    gm.open(options) ;
  }
  ListenerMsg.init = (user) => {
    if (!user) {
      alert("未知的當前登錄人") ;
      return ;
    }
    let url = `ws://localhost:8080/message/${user}` ;
    let msg = document.querySelector("#msg")
    ListenerMsg({url, options: {
      onmessage (e) {
        let data = JSON.parse(e.data) ;
        let li = document.createElement("li") ;
        li.innerHTML = "【" + data.fromName + "】對你說:" + data.content ;
        msg.appendChild(li) ;
      }
    }}) ;
  }
  function enter() {
    username = document.querySelector("#nick").value ;
    ListenerMsg.init(username) ;
    document.querySelector("#chat").style.display = "block" ;
    document.querySelector("#enter").style.display = "none" ;
    document.querySelector("#cu").innerText = username ;
  }
  function send() {
    let a = document.querySelector("#toname") ;
    let b = document.querySelector("#content") ;
    let toName = a.value ;
    let content = b.value ;
    gm.sendMessage({type: "2001", content, fromName: username, toName}) ;
    a.value = '' ;
    b.value = '' ;
  }
</script>
 </head>
 <body>
  <div id="enter">
    <input id="nick"/><button type="button" onclick="enter()">進入</button>
  </div>
  <hr/>
  <div id="chat" style="display:none;">
    當前用戶:<b id="cu"></b><br/>
    用戶:<input id="toname" name="toname"/><br/><br/>
    內容:<textarea id="content" rows="3" cols="22"></textarea><br/>
    <button type="button" onclick="send()">發(fā)送</button>
  </div>
  <div>
    <ul id="msg">
    </ul>
  </div>
 </body>
</html>

這里有個g-messages.js文件是我寫的一個工具類,用來做連接及心跳檢查用的。

到此所有的代碼完畢,接下來測試

測試

打開兩個標簽頁,以不同的用戶進入。

圖片


圖片


輸入對方用戶名發(fā)送消息

圖片


圖片


成功了,簡單的websocket。我們生產環(huán)境還就這么完的,8g內存跑了6w的用戶。

責任編輯:武曉燕 來源: Spring全家桶實戰(zhàn)案例源碼
相關推薦

2021-03-25 08:29:33

SpringBootWebSocket即時消息

2020-10-09 12:45:19

創(chuàng)建消息即時消息編程語言

2020-10-09 15:00:56

實時消息編程語言

2019-09-29 15:25:13

CockroachDBGoJavaScript

2019-10-28 20:12:40

OAuthGuard中間件編程語言

2020-03-31 12:21:20

JSON即時消息編程語言

2020-10-12 09:20:13

即時消息Access頁面編程語言

2020-10-19 16:20:38

即時消息Conversatio編程語言

2020-10-16 14:40:20

即時消息Home頁面編程語言

2015-03-18 15:37:19

社交APP場景

2021-02-05 07:28:11

SpringbootNettyWebsocke

2022-06-28 08:37:07

分布式服務器WebSocket

2020-10-10 20:51:10

即時消息編程語言

2023-07-26 07:28:55

WebSocket服務器方案

2021-03-26 08:16:32

SpringbootWebsocket前端

2024-09-02 09:31:19

2010-05-24 09:51:37

System Cent

2024-09-12 14:50:08

2024-11-14 11:56:45

2024-09-11 08:35:54

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲天堂色| 天天拍夜夜爽 | 99久久亚洲| 99看片网 | 国产在线第一页 | 91精品久久久久久久久 | 亚洲综合视频 | 国产一二区视频 | 久草综合在线视频 | 亚洲一区高清 | 国产乱码高清区二区三区在线 | xxx国产精品视频 | 国产综合精品一区二区三区 | 日韩在线 | 丝袜毛片 | 精品国产乱码久久久久久图片 | 精品国产一区二区三区久久 | 日本久久久久久 | 精品九九 | 免费在线国产视频 | 日韩一区中文字幕 | 欧美一区二区大片 | 日韩h| 国产在线精品区 | 日韩精品一区二区三区四区视频 | 九九九视频精品 | 91视频播放 | 三级在线视频 | 精品伊人| 香蕉av免费 | 亚洲一区二区中文字幕 | 久久免费精品视频 | 亚洲在线一区二区三区 | 欧美日韩a | 午夜精品在线观看 | 日韩一区二区在线视频 | 欧美一区二区三区日韩 | 羞羞视频在线观看网站 | 国产成人精品午夜视频免费 | 亚洲成人999 | 成年网站在线观看 |