使用websocket让服务器端给客户端推数据

桃扇骨 2022-02-19 13:57 396阅读 0赞

使用websocket让服务器端给客户端推数据

  • 1 背景
  • 2 搭建dome
    • 2.0 项目结构
    • 2.1 创建一个springboot项目
    • 2.2 添加maven依赖
    • 2.3 开启WebSocket支持
    • 2.4 消息推送
    • 2.5 前端发起socket连接请求
    • 2.6 原理图

1 背景

最近对websocket比较感兴趣,一直在想http都是客户端向服务端主动发请求,然后进行数据交互。但是如何让服务器端主动向客户端发送数据或者推送事件呢?
很神奇,比较好奇,带着这样的疑问,就来简单了解下websocket。

准备分三步学习:

  • 快速使用springboot搭建一个dome,先感受websocket的神奇
  • 做一个1对1(好友聊天),1对多(群聊)的demo
  • 分析源码

本篇文章做第一步,使用springboot搭建一个dome

2 搭建dome

2.0 项目结构

项目结构

2.1 创建一个springboot项目

  • 例如:我创建了一个名为websocket的项目;包名为com.wllfengshu

2.2 添加maven依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-parent</artifactId>
  7. <version>2.1.4.RELEASE</version>
  8. <relativePath/> <!-- lookup parent from repository -->
  9. </parent>
  10. <groupId>com.wllfengshu</groupId>
  11. <artifactId>websocket</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <name>websocket</name>
  14. <description>Demo project for Spring Boot</description>
  15. <properties>
  16. <java.version>1.8</java.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter</artifactId>
  22. </dependency>
  23. <!--添加websocket依赖-->
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-websocket</artifactId>
  27. </dependency>
  28. </dependencies>
  29. <build>
  30. <plugins>
  31. <plugin>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-maven-plugin</artifactId>
  34. </plugin>
  35. </plugins>
  36. </build>
  37. </project>

2.3 开启WebSocket支持

  1. package com.wllfengshu.websocket.configs;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.socket.server.standard.ServerEndpointExporter;
  5. /** * 开启WebSocket支持 * @author */
  6. @Configuration
  7. public class WebSocketConfig {
  8. @Bean
  9. public ServerEndpointExporter serverEndpointExporter() {
  10. return new ServerEndpointExporter();
  11. }
  12. }

2.4 消息推送

  • 有两种办法,一是:直接把ServerEndpoint当作一个控制器,加上一个@RestController注解即可;二是:把ServerEndpoint当作一个组件,加上一个@Component注解,然后在业务层调用。具体的可以看参考文献,他们都写的很好。(至于使用拦截器的方式太麻烦,暂不研究)
  • 本文列举第一种,后期的“聊天”功能也基于第一种方法

    package com.wllfengshu.websocket.rest;

    import java.util.concurrent.CopyOnWriteArraySet;

    import javax.websocket.OnClose;
    import javax.websocket.OnError;
    import javax.websocket.OnMessage;
    import javax.websocket.OnOpen;
    import javax.websocket.Session;
    import javax.websocket.server.PathParam;
    import javax.websocket.server.ServerEndpoint;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.bind.annotation.RestController;

    //这里的/webSocket/{id}/{name}只是拦截的url,不一定要是这个,你写/aaa也是行的
    @ServerEndpoint(“/webSocket/{id}/{name}”)
    @RestController
    public class TestWebSocket {

    1. /** * 用来记录当前连接数的变量 */
    2. private static volatile int onlineCount = 0;
    3. /** * concurrent包的线程安全Set,用来存放每个客户端对应的WebSocket对象 */
    4. private static CopyOnWriteArraySet<TestWebSocket> webSocketSet = new CopyOnWriteArraySet<>();
    5. /** * 与某个客户端的连接会话,需要通过它来与客户端进行数据收发 */
    6. private Session session;
    7. private static final Logger logger = LoggerFactory.getLogger(TestWebSocket.class);
    8. @OnOpen
    9. public void onOpen(Session session, @PathParam("id") long id, @PathParam("name") String name) throws Exception {
    10. this.session = session;
    11. System.out.println(this.session.getId());
    12. webSocketSet.add(this);
    13. logger.info("Open a websocket. id={}, name={}", id, name);
    14. }
    15. @OnClose
    16. public void onClose() {
    17. webSocketSet.remove(this);
    18. logger.info("Close a websocket. ");
    19. }
    20. @OnMessage
    21. public void onMessage(String message, Session session) {
    22. logger.info("Receive a message from client: " + message);
    23. try {
    24. //接受到客户端信息后,立即回复“我是服务器,收到数据了”
    25. sendMessage("我是服务器,收到数据了");
    26. }catch (Exception e){
    27. logger.error("Error sendMessage. ", e);
    28. }
    29. }
    30. @OnError
    31. public void onError(Session session, Throwable error) {
    32. logger.error("Error while websocket. ", error);
    33. }
    34. // 发送消息
    35. public void sendMessage(String message) throws Exception {
    36. if (this.session.isOpen()) {
    37. this.session.getBasicRemote().sendText(message);
    38. }
    39. }
    40. public static synchronized int getOnlineCount() {
    41. return onlineCount;
    42. }
    43. public static synchronized void addOnlineCount() {
    44. TestWebSocket.onlineCount++;
    45. }
    46. public static synchronized void subOnlineCount() {
    47. TestWebSocket.onlineCount--;
    48. }

    }

2.5 前端发起socket连接请求

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <script src="https://cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script>
  6. <title>Java后端WebSocket实现</title>
  7. </head>
  8. <body>
  9. 信息框:<input id="text" type="text"/>
  10. <button onclick="send()">发送</button>
  11. <hr/>
  12. <button onclick="closeWebSocket()">关闭WebSocket连接</button>
  13. <hr/>
  14. 收到的信息如下:</br>
  15. <div id="message">
  16. </div>
  17. </body>
  18. <script type="text/javascript"> if(typeof(WebSocket) == "undefined") { alert("您的浏览器不支持WebSocket"); } var webSocket = new WebSocket("ws://127.0.0.1:8080/webSocket/1/liang"); //连接发生错误的回调方法 webSocket.onerror = function () { setMessageInnerHTML("WebSocket连接发生错误"); }; //连接成功建立的回调方法 webSocket.onopen = function () { setMessageInnerHTML("WebSocket连接成功"); } //接收到消息的回调方法 webSocket.onmessage = function (event) { setMessageInnerHTML(event.data); } //连接关闭的回调方法 webSocket.onclose = function () { setMessageInnerHTML("WebSocket连接关闭"); } //监听窗口关闭事件,当窗口关闭时,主动去关闭webSocket连接,防止连接还没断开就关闭窗口,server端会抛异常。 window.onbeforeunload = function () { closeWebSocket(); } //关闭WebSocket连接 function closeWebSocket() { webSocket.close(); } //发送消息 function send() { var message = $('#text').val(); webSocket.send(message); $("#text").val(""); } //将消息显示在网页上 var val = ""; function setMessageInnerHTML(innerHTML) { var temp = '<br/>' + innerHTML + '<br/>'; val = val + temp; $("#message").html(val); } </script>
  19. </html>

2.6 原理图

Created with Raphaël 2.2.0开始页面发起socket连接TestWebSocket控制器拦截请求执行@OnOpen修饰的方法当浏览器发送信息时,会执行@OnMessage方法当浏览器发送断开连接,会执行@OnClose方法如果连接中断,会执行@OnError方法结束

参考:
[1] https://www.cnblogs.com/strugglion/p/10021173.html
[2] https://blog.csdn.net/moshowgame/article/details/80275084

发表评论

表情:
评论列表 (有 0 条评论,396人围观)

还没有评论,来说两句吧...

相关阅读