SpringBoot进阶教程(七十七)WebSocket

金歌  金牌会员 | 2023-10-11 16:29:32 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 939|帖子 939|积分 2817

WebSocket是一种在单个TCP连接上进行全双工通信的协议。WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在WebSocket API中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进行双向数据传输。
v原理

很多网站为了实现推送技术,所用的技术都是轮询。轮询是在特定的时间间隔(如每1秒),由浏览器对服务器发出HTTP请求,然后由服务器返回最新的数据给客户端的浏览器。这种传统的模式带来很明显的缺点,即浏览器需要不断的向服务器发出请求,然而HTTP请求可能包含较长的头部,其中真正有效的数据可能只是很小的一部分,显然这样会浪费很多的带宽等资源。
而比较新的技术去做轮询的效果是Comet。这种技术虽然可以双向通信,但依然需要反复发出请求。而且在Comet中,普遍采用的长链接,也会消耗服务器资源。
在这种情况下,HTML5定义了WebSocket协议,能更好的节省服务器资源和带宽,并且能够更实时地进行通讯。
v架构搭建

添加maven引用
  1.         <dependency>
  2.             <groupId>org.springframework.boot</groupId>
  3.             <artifactId>spring-boot-starter-websocket</artifactId>
  4.         </dependency>
  5.         <dependency>
  6.             <groupId>org.springframework.boot</groupId>
  7.             <artifactId>spring-boot-starter-thymeleaf</artifactId>
  8.         </dependency>
复制代码
配置应用属性
  1. server.port=8300
  2. spring.thymeleaf.mode=HTML
  3. spring.thymeleaf.cache=true
  4. spring.thymeleaf.prefix=classpath:/web/
  5. spring.thymeleaf.encoding: UTF-8
  6. spring.thymeleaf.suffix: .html
  7. spring.thymeleaf.check-template-location: true
  8. spring.thymeleaf.template-resolver-order: 1
复制代码
添加WebSocketConfig
  1. package com.test.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.socket.config.annotation.EnableWebSocket;
  5. import org.springframework.web.socket.server.standard.ServerEndpointExporter;
  6. /**
  7. * @Author chen bo
  8. * @Date 2023/10
  9. * @Des
  10. */
  11. @Configuration
  12. public class WebSocketConfig {
  13.     /**
  14.      * bean注册:会自动扫描带有@ServerEndpoint注解声明的Websocket Endpoint(端点),注册成为Websocket bean。
  15.      * 要注意,如果项目使用外置的servlet容器,而不是直接使用springboot内置容器的话,就不要注入ServerEndpointExporter,因为它将由容器自己提供和管理。
  16.      */
  17.     @Bean
  18.     public ServerEndpointExporter serverEndpointExporter() {
  19.         return new ServerEndpointExporter();
  20.     }
  21. }
复制代码
添加WebSocket核心类
因为WebSocket是类似客户端服务端的形式(采用ws协议),那么这里的WebSocketServer其实就相当于一个ws协议的Controller
直接@ServerEndpoint("/imserver/{userId}") 、@Component启用即可,然后在里面实现@OnOpen开启连接,@onClose关闭连接,@onMessage接收消息等方法。
新建一个ConcurrentHashMap用于接收当前userId的WebSocket或者Session信息,方便IM之间对userId进行推送消息。单机版实现到这里就可以。集群版(多个ws节点)还需要借助 MySQL或者 Redis等进行订阅广播方式处理,改造对应的 sendMessage方法即可。
  1. package com.test.util;
  2. import com.google.gson.JsonParser;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.stereotype.Component;
  5. import javax.websocket.*;
  6. import javax.websocket.server.PathParam;
  7. import javax.websocket.server.ServerEndpoint;
  8. import java.util.Map;
  9. import java.util.concurrent.ConcurrentHashMap;
  10. import java.util.concurrent.atomic.AtomicInteger;
  11. import com.google.gson.JsonObject;
  12. /**
  13. * WebSocket的操作类
  14. * html页面与之关联的接口
  15. * var reqUrl = "http://localhost:8300/websocket/" + cid;
  16. * socket = new WebSocket(reqUrl.replace("http", "ws"));
  17. */
  18. @Component
  19. @Slf4j
  20. @ServerEndpoint("/websocket/{sid}")
  21. public class WebSocketServer {
  22.     /**
  23.      * 静态变量,用来记录当前在线连接数,线程安全的类。
  24.      */
  25.     private static AtomicInteger onlineSessionClientCount = new AtomicInteger(0);
  26.     /**
  27.      * 存放所有在线的客户端
  28.      */
  29.     private static Map<String, Session> onlineSessionClientMap = new ConcurrentHashMap<>();
  30.     /**
  31.      * 连接sid和连接会话
  32.      */
  33.     private String sid;
  34.     private Session session;
  35.     /**
  36.      * 连接建立成功调用的方法。由前端new WebSocket触发
  37.      *
  38.      * @param sid     每次页面建立连接时传入到服务端的id,比如用户id等。可以自定义。
  39.      * @param session 与某个客户端的连接会话,需要通过它来给客户端发送消息
  40.      */
  41.     @OnOpen
  42.     public void onOpen(@PathParam("sid") String sid, Session session) {
  43.         /**
  44.          * session.getId():当前session会话会自动生成一个id,从0开始累加的。
  45.          */
  46.         log.info("连接建立中 ==> session_id = {}, sid = {}", session.getId(), sid);
  47.         //加入 Map中。将页面的sid和session绑定或者session.getId()与session
  48.         //onlineSessionIdClientMap.put(session.getId(), session);
  49.         onlineSessionClientMap.put(sid, session);
  50.         //在线数加1
  51.         onlineSessionClientCount.incrementAndGet();
  52.         this.sid = sid;
  53.         this.session = session;
  54.         sendToOne(sid, "上线了");
  55.         log.info("连接建立成功,当前在线数为:{} ==> 开始监听新连接:session_id = {}, sid = {},。", onlineSessionClientCount, session.getId(), sid);
  56.     }
  57.     /**
  58.      * 连接关闭调用的方法。由前端socket.close()触发
  59.      *
  60.      * @param sid
  61.      * @param session
  62.      */
  63.     @OnClose
  64.     public void onClose(@PathParam("sid") String sid, Session session) {
  65.         //onlineSessionIdClientMap.remove(session.getId());
  66.         // 从 Map中移除
  67.         onlineSessionClientMap.remove(sid);
  68.         //在线数减1
  69.         onlineSessionClientCount.decrementAndGet();
  70.         log.info("连接关闭成功,当前在线数为:{} ==> 关闭该连接信息:session_id = {}, sid = {},。", onlineSessionClientCount, session.getId(), sid);
  71.     }
  72.     /**
  73.      * 收到客户端消息后调用的方法。由前端socket.send触发
  74.      * * 当服务端执行toSession.getAsyncRemote().sendText(xxx)后,前端的socket.onmessage得到监听。
  75.      *
  76.      * @param message
  77.      * @param session
  78.      */
  79.     @OnMessage
  80.     public void onMessage(String message, Session session) {
  81.         /**
  82.          * html界面传递来得数据格式,可以自定义.
  83.          * {"sid":"user","message":"hello websocket"}
  84.          */
  85.         JsonObject jsonObject = JsonParser.parseString(message).getAsJsonObject();
  86.         String toSid = jsonObject.get("sid").getAsString();
  87.         String msg = jsonObject.get("message").getAsString();
  88.         log.info("服务端收到客户端消息 ==> fromSid = {}, toSid = {}, message = {}", sid, toSid, message);
  89.         /**
  90.          * 模拟约定:如果未指定sid信息,则群发,否则就单独发送
  91.          */
  92.         if (toSid == null || toSid == "" || "".equalsIgnoreCase(toSid)) {
  93.             sendToAll(msg);
  94.         } else {
  95.             sendToOne(toSid, msg);
  96.         }
  97.     }
  98.     /**
  99.      * 发生错误调用的方法
  100.      *
  101.      * @param session
  102.      * @param error
  103.      */
  104.     @OnError
  105.     public void onError(Session session, Throwable error) {
  106.         log.error("WebSocket发生错误,错误信息为:" + error.getMessage());
  107.         error.printStackTrace();
  108.     }
  109.     /**
  110.      * 群发消息
  111.      *
  112.      * @param message 消息
  113.      */
  114.     private void sendToAll(String message) {
  115.         // 遍历在线map集合
  116.         onlineSessionClientMap.forEach((onlineSid, toSession) -> {
  117.             // 排除掉自己
  118.             if (!sid.equalsIgnoreCase(onlineSid)) {
  119.                 log.info("服务端给客户端群发消息 ==> sid = {}, toSid = {}, message = {}", sid, onlineSid, message);
  120.                 toSession.getAsyncRemote().sendText(message);
  121.             }
  122.         });
  123.     }
  124.     /**
  125.      * 指定发送消息
  126.      *
  127.      * @param toSid
  128.      * @param message
  129.      */
  130.     private void sendToOne(String toSid, String message) {
  131.         // 通过sid查询map中是否存在
  132.         Session toSession = onlineSessionClientMap.get(toSid);
  133.         if (toSession == null) {
  134.             log.error("服务端给客户端发送消息 ==> toSid = {} 不存在, message = {}", toSid, message);
  135.             return;
  136.         }
  137.         // 异步发送
  138.         log.info("服务端给客户端发送消息 ==> toSid = {}, message = {}", toSid, message);
  139.         toSession.getAsyncRemote().sendText(message);
  140.         /*
  141.         // 同步发送
  142.         try {
  143.             toSession.getBasicRemote().sendText(message);
  144.         } catch (IOException e) {
  145.             log.error("发送消息失败,WebSocket IO异常");
  146.             e.printStackTrace();
  147.         }*/
  148.     }
  149. }
复制代码
添加controller
  1. package com.test.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.Model;
  4. import org.springframework.web.bind.annotation.*;
  5. import javax.servlet.http.HttpServletResponse;
  6. /**
  7. * @Author chen bo
  8. * @Date 2023/10
  9. * @Des
  10. */
  11. @Controller
  12. public class HomeController {
  13.     /**
  14.      * 跳转到websocketDemo.html页面,携带自定义的cid信息。
  15.      * http://localhost:8300/demo/toWebSocketDemo/user
  16.      *
  17.      * @param cid
  18.      * @param model
  19.      * @return
  20.      */
  21.     @GetMapping("/demo/toWebSocketDemo/{cid}")
  22.     public String toWebSocketDemo(@PathVariable String cid, Model model) {
  23.         model.addAttribute("cid", cid);
  24.         return "index";
  25.     }
  26.     @GetMapping("hello")
  27.     @ResponseBody
  28.     public String hi(HttpServletResponse response) {
  29.         return "Hi";
  30.     }
  31. }
复制代码
添加html
注意:html文件添加在application.properties配置的对应目录中。
  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>聊天窗口</title>
  6. </head>
  7. <body>
  8. 我的用户名:
  9. <input type="text" th:value="${cid}" readonly="readonly" id="cid"/>
  10. 收消息人用户名:<input id="toUserId" name="toUserId" type="text">
  11. 输入你要说的话:<input id="contentText" name="contentText" type="text">
  12.     <button type="button" onclick="sendMessage()">发送消息</button>
  13. </body>
  14. </html>
复制代码
1对1模拟演练
启动项目后,在浏览器访问http://localhost:8300/demo/toWebSocketDemo/{cid} 跳转到对应页面,其中cid是用户名。
为了便于1对1测试,这里我们启动两个浏览器窗口。
http://localhost:8300/demo/toWebSocketDemo/阳光男孩
http://localhost:8300/demo/toWebSocketDemo/水晶女孩
按照要求输入对方用户信息之后,便可以输入你要说的话,畅快聊起来了。
效果图如下:

当然,如果收消息人用户名是自己的话,也可以自己给自己发送数据的。
群发模拟演练
为了便于群发测试,这里我们启动3个浏览器窗口。
http://localhost:8300/demo/toWebSocketDemo/阳光男孩
http://localhost:8300/demo/toWebSocketDemo/水晶女孩
http://localhost:8300/demo/toWebSocketDemo/路人A
由于sendToAll方法中定义群发的条件为:当不指定 toUserid时,则为群发。
效果图如下:

项目架构图如下:

v源码地址

https://github.com/toutouge/javademosecond
其他参考/学习资料:

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

金歌

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表