springboot整合websocket的基础知识点

  介绍

这篇文章给大家分享的是有关springboot整合websocket的基础知识点的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

<强>项目最终的文件结构

 springboot整合websocket的基础知识点“> <br/> </p> <p> 1添加maven依赖</p> <pre类=, & lt; dependency>   ,,,,,& lt; groupId> org.springframework.boot   ,,,,,& lt; artifactId> spring-boot-starter-websocket   & lt;/dependency>   & lt; dependency>   ,,,,,& lt; groupId> org.projectlombok   ,,,,,& lt; artifactId> lombok   ,,,,,& lt; version> 1.18.12   & lt;/dependency>

2编写配置类,WebSocketConfig

package  cn.huawei.socket_test_1.config;   ,   import  org.springframework.context.annotation.Bean;   import  org.springframework.stereotype.Component;   import  org.springframework.web.socket.server.standard.ServerEndpointExporter;   ,   @ component   public  class  WebSocketConfig  {   ,   ,/* *   ,,*,ServerEndpointExporter 作用   ,,*   ,,*,这个Bean会自动注册使用@ServerEndpoint注解声明的websocket 端点   ,,*   ,,* @return   ,,*/,@ bean   public 才能;ServerEndpointExporter  serverEndpointExporter (), {   ,,,return  new  ServerEndpointExporter ();   ,,}   }

3编写核心业务类,WebSocket

package  cn.huawei.socket_test_1.websock;   import  lombok.extern.slf4j.Slf4j;   import  org.springframework.stereotype.Component;   ,   import  javax.websocket。*;   import  javax.websocket.server.PathParam;   import  javax.websocket.server.ServerEndpoint;   import  java.util.concurrent.ConcurrentHashMap;   ,/* *   ,* @ServerEndpoint 注解的作用   ,*   ,* @ServerEndpoint 注解是一个类层次的注解,它的功能主要是将目前的类定义成一个websocket服务器端,   ,*注解的值将被用于监听用户连接的终端访问URL地址,客户端可以通过这个URL来连接到WebSocket服务器端   ,*/,   @Slf4j   @ component   @ServerEndpoint (“/websocket/{名称}“)   public  class  WebSocket  {   ,   ,/* *   ,,*,与某个客户端的连接对话,需要通过它来给客户端发送消息   ,,*/private 才能Session 会话;   ,   ,/* *   ,,*,标识当前连接客户端的用户名   ,,*/private 才能;String 名称;   ,   ,/* *   ,,*,用于存所有的连接服务的客户端,这个对象存储是安全的   ,,*,注意这里的kv,设计的很巧妙,v刚好是本类,WebSocket (用来存放每个客户端对应的MyWebSocket对象)   ,,*/private 才能;static  ConcurrentHashMap<字符串,WebSocket>, webSocketSet =, new  ConcurrentHashMap<在();   ,   ,   ,/* *   ,,*,连接建立成功调用的方法   ,,*,会话为与某个客户端的连接会话,需要通过它来给客户端发送数据   ,,*/@OnOpen才能   public 才能;void  OnOpen (Session 会话,@PathParam (=value “name"), String 名称){   ,,,log.info (“- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -“);   ,,,this.session =,会话;   ,,,this.name =,名称;   ,,,//,名字是用来表示唯一客户端,如果需要指定发送,需要指定发送通过名字来区分   ,,,webSocketSet.put(名字,这个);   ,,,log.info (“[WebSocket],连接成功,当前连接人数为:={}“,webSocketSet.size ());   ,,,log.info (“- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -“);   ,,,log.info (“;”);   ,   ,,,GroupSending(名字+“,来了“);   ,,}   ,   ,/* *   ,,*,连接关闭调用的方法   ,,*/@OnClose才能   public 才能;void  OnClose () {   ,,,webSocketSet.remove (this.name);   ,,,log.info (“[WebSocket],退出成功,当前连接人数为:={}“,webSocketSet.size ());   ,   ,,,GroupSending(名字+“,走了“);   ,,}   ,   ,/* *   ,,*,收到客户端消息后调用的方法   ,,*/@OnMessage才能   public 才能;void  OnMessage (String  message_str) {   ,,,log.info (“[WebSocket],收到消息:{}“,message_str);   ,,,//判断是否需要指定发送,具体规则自定义   ,,,//message_str的格式,用:user2;信息:aaaaaaaaaaaaaaaaaa;   ,,,如果(message_str.indexOf (“TOUSER"),==, 0) {   ,,,,,//取出,名称和消息的值   ,,,,,String [], split =, message_str.split (“;”);   ,,,,,String [], split1 =,分裂[0].split (“:”);   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

springboot整合websocket的基础知识点