spring boot 2.0 搭建 websocket 实现消息推送

spring boot 2.0 搭建 websocket 实现消息推送
WebSocket协议是基于TCP的一种新的网络协议。它实现了浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端。
初次接触 WebSocket 的人,都会问同样的问题:我们已经有了 HTTP 协议,为什么还需要另一个协议?它能带来什么好处?
答案很简单,因为 HTTP 协议有一个缺陷:通信只能由客户端发起,HTTP 协议做不到服务器主动向客户端推送信息。
举例来说,我们想要查询当前的排队情况,只能是页面轮询向服务器发出请求,服务器返回查询结果。轮询的效率低,非常浪费资源(因为必须不停连接,或者 HTTP 连接始终打开)。因此WebSocket 就是这样发明的。

话不多说:

创建 spring_boot_socket
在这里插入图片描述

修改:

 <?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.spring_websocket</groupId>
    <artifactId>spring_boot_websocket</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring_boot_websocket</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.68</version>
        </dependency>


        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

增加配置文件:

package com.spring_websocket.spring_boot_websocket.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * 开启WebSocket支持
 * @author yulizi
 */
@Configuration
public class WebSocketConfig {

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

}

在这里插入图片描述

WebSocketServer
这就是重点了,核心都在这里。

因为WebSocket是类似客户端服务端的形式(采用ws协议),那么这里的WebSocketServer其实就相当于一个ws协议的Controller

直接

@ServerEndpoint("/imserver/{userId}") 、
@Component启用即可,然后在里面实现
@OnOpen开启连接,
@onClose关闭连接,
@onMessage接收消息等方法。

新建一个ConcurrentHashMap webSocketMap 用于接收当前userId的WebSocket,方便IM之间对userId进行推送消息。单机版实现到这里就可以。
集群版(多个ws节点)还需要借助mysql或者redis等进行处理,改造对应的sendMessage方法即可。

模拟手动触发后台的任务: WebSocketController 正常任务可以为后台业务调用.

package com.spring_websocket.spring_boot_websocket.config;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.io.IOException;

/**
 * WebSocketController
 * @author yulizi
 */
@RestController
public class WebSocketController {

    //这个是通过手动触发的,现实中应该通过程序触发 ,例如定时器.
    @RequestMapping("/push/{toUserId}")
    public ResponseEntity<String> pushToWeb(@PathVariable String toUserId) throws IOException {
        String message= toUserId+ "你应该去获取东西去了!" ;
        WebSocketServer.sendInfo(message,toUserId);
        return ResponseEntity.ok("MSG SEND SUCCESS");
    }
}

WebSocketServer 服务

package com.spring_websocket.spring_boot_websocket.config;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
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 com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


/**
 * @author yulizi
 */
@ServerEndpoint("/imserver/{userId}")
@Component
public class WebSocketServer {

    static Logger log = LoggerFactory.getLogger(WebSocketServer.class);

    /**静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。*/
    private static int onlineCount = 0;
    /**concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。*/
    private static ConcurrentHashMap<String,WebSocketServer> webSocketMap = new ConcurrentHashMap<>();
    /**与某个客户端的连接会话,需要通过它来给客户端发送数据*/
    private Session session;
    /**接收userId*/
    private String userId="";

    /**
     * 连接建立成功调用的方法*/
    @OnOpen
    public void onOpen(Session session,@PathParam("userId") String userId) {

        log.info("用户连接:"+userId+",当前在线人数为:" + getOnlineCount());
        this.session = session;
        this.userId=userId;
        if(webSocketMap.containsKey(userId)){
            webSocketMap.remove(userId);
            webSocketMap.put(userId,this);
            //加入set中
        }else{
            webSocketMap.put(userId,this);
            //加入set中
            addOnlineCount();
            //在线数加1
        }
        try {
            sendMessage("连接成功");
        } catch (IOException e) {
            log.error("用户:"+userId+",网络异常!!!!!!");
        }
    }

    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose() {
        log.info("用户退出:"+userId+",当前在线人数为:" + getOnlineCount());
        if(webSocketMap.containsKey(userId)){
            webSocketMap.remove(userId);
            //从set中删除
            subOnlineCount();
        }
    }

    /**
     * 收到客户端消息后调用的方法
     *
     * @param message 客户端发送过来的消息*/
    @OnMessage
    public void onMessage(String message, Session session) {
        //可以群发消息
        //消息保存到数据库、redis
        if(StringUtils.isNotBlank(message)){
            try {
                //解析发送的报文
                JSONObject jsonObject = JSON.parseObject(message);
                //追加发送人(防止串改)
                jsonObject.put("fromUserId",this.userId);
                String toUserId=jsonObject.getString("toUserId");
                //传送给对应toUserId用户的websocket
                if(StringUtils.isNotBlank(toUserId)&&webSocketMap.containsKey(toUserId)){
                    webSocketMap.get(toUserId).sendMessage(jsonObject.getString("contentText"));
                }else{
                    log.error("请求的userId:"+toUserId+"不在该服务器上");
                    //否则不在这个服务器上,发送到mysql或者redis
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    /**
     *
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {
        log.error("用户错误:"+this.userId+",原因:"+error.getMessage());
        error.printStackTrace();
    }
    /**
     * 实现服务器主动推送
     */
    public void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);
    }

    /**
     * 发送自定义消息
     * */
    public static void sendInfo(String message,@PathParam("userId") String userId) throws IOException {
        log.info("发送消息到:"+userId+",报文:"+message);
        if(StringUtils.isNotBlank(userId)&&webSocketMap.containsKey(userId)){
            webSocketMap.get(userId).sendMessage(message);
        }else{
            log.error("用户"+userId+",不在线!");
        }
    }

    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    public static synchronized void addOnlineCount() {
        WebSocketServer.onlineCount++;
    }

    public static synchronized void subOnlineCount() {
        WebSocketServer.onlineCount--;
    }
}

client.html 页面

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>websocket通讯</title>
</head>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script>

    //打开pad直接上线.
	
    var socket;
    function openSocket() {
        if(typeof(WebSocket) == "undefined") {
            console.log("您的浏览器不支持WebSocket");
        }else{
		
		    $("#info").show();
			$("#login").hide();
			
            console.log("您的浏览器支持WebSocket");
            var socketUrl="http://localhost:9100/imserver/"+$("#userId").val();
            socketUrl=socketUrl.replace("https","ws").replace("http","ws");
            console.log(socketUrl);
            if(socket!=null){
                socket.close();
                socket=null;
            }
            socket = new WebSocket(socketUrl);
            socket.onopen = function() {
                console.log("websocket已打开");
                //socket.send("这是来自客户端的消息" + location.href + new Date());
            };
            socket.onmessage = function(msg) {
                console.log(msg.data);
				$("#contentText_zhangsan").val(msg.data) ;
            };
            socket.onclose = function() {
                console.log("websocket已关闭");
            };
            socket.onerror = function() {
                console.log("websocket发生了错误");
            }
        }
    }
 
    //确认知道消息
	function  enter_infomation() {
        if(typeof(WebSocket) == "undefined") {
            console.log("您的浏览器不支持WebSocket");
        }else {
		    $("#contentText_zhangsan").val();
            //console.log("您的浏览器支持WebSocket");
            //console.log('{"toUserId":"'+$("#userId").val()+'","contentText":"'+$("#contentText").val()+'"}');
            socket.send('{"toUserId":"'+$("#userId").val()+'","contentText":"1234567890"}');
        }
    }
	 
</script>
<body>

<div  style="display:none" id="info" >
<p><div ><textarea id="contentText_zhangsan"> </textarea></div>
<p><div ><a onClick="enter_infomation()"> <div style="width:100px; border:2px solid #999999; text-align:center"> 确认 </div></a></div>
</div>

<div  style="display:block" id="login" >
<p>【输入账号】:<div><input id="userId" name="userId" type="text" value="zhangsan"></div>
<p><div><a onClick="openSocket()"> <div style="width:100px; border:2px solid #999999; text-align:center"> 登录</div></a></div>
</div>
</body>

</html>

最终项目的目录结构:

在这里插入图片描述

前端模拟地址: 我在tomcat 里面增加了一个文件夹: websocketclient 里面放了一个文件 client.html
先登录 输入 zhangsan 的帐号 ,这样就在 websocket 中注册帐号.
http://192.168.8.147:5199/websocketclient/client.html

我的后台地址默认启动.没有配置项目路径只配置了端口: 9100
后台模拟地址: 推送数据给前台得 zhangsan .
http://192.168.8.147:9100/push/zhangsan

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot 2.0 是一套快速开发框架,其中包含了 WebSocket 模块,能够轻松地集成 WebSocket实现服务端主动向前端推送数据。 首先,在pom.xml文件中引入Spring Boot的Starter Websocket依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> 然后,在Spring Boot的启动类上使用@EnableWebSocket注解开启 WebSocket: @SpringBootApplication @EnableWebSocket public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } 接着,编写一个 WebSocketEndpoint 类用于处理 WebSocket 连接和消息的收发: @Component public class WebSocketEndpoint implements WebSocketHandler { private static final List<WebSocketSession> SESSIONS = new CopyOnWriteArrayList<>(); @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { SESSIONS.add(session); } @Override public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception { // 接收到消息 } @Override public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { SESSIONS.remove(session); } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { SESSIONS.remove(session); } @Override public boolean supportsPartialMessages() { return false; } // 服务器向前端推送消息 public static void sendMessage(String message) throws IOException { for (WebSocketSession session : SESSIONS) { if (session.isOpen()) { session.sendMessage(new TextMessage(message)); } } } } 最后,当服务端需要向前端推送数据时,可以调用 WebSocketEndpoint 中的 sendMessage 方法: WebSocketEndpoint.sendMessage("Hello, websocket!"); 前端则需要开启 WebSocket 连接,并在 onmessage 方法中接收服务端推送的数据: var socket = new WebSocket("ws://localhost:8080/websocket"); socket.onmessage = function(event) { var data = event.data; // 处理服务端推送的数据 }; 总之,Spring Boot 2.0 整合 WebSocket 实现服务端主动向前端推送数据非常简单,只需要几行代码即可实现。 ### 回答2: Spring Boot 2.0 提供了与 WebSocket 相关的依赖和注解,可以方便地实现与前端的实时通信。下面介绍如何使用 Spring Boot 2.0 整合 WebSocket 实现服务器主动推送数据到前端。 首先,在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> ``` 然后,创建一个 WebSocket 配置类,使用 `@EnableWebSocket` 注解启用 WebSocket: ``` @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(myWebSocketHandler(), "/websocket").setAllowedOrigins("*"); } @Bean public WebSocketHandler myWebSocketHandler() { return new MyWebSocketHandler(); } } ``` 其中 `MyWebSocketHandler` 是自己实现WebSocket 消息处理类,可以在其中实现处理客户端发送过来的消息以及向客户端发送消息的逻辑。 下面是 `MyWebSocketHandler` 的一个示例: ``` public class MyWebSocketHandler extends TextWebSocketHandler { private Set<WebSocketSession> sessions = new CopyOnWriteArraySet<>(); @Override public void afterConnectionEstablished(WebSocketSession session) { sessions.add(session); } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) { sessions.remove(session); } @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) { // 处理客户端发送过来的消息 } public void pushMessage(String message) { for (WebSocketSession session : sessions) { try { session.sendMessage(new TextMessage(message)); } catch (IOException e) { // 发送消息失败 } } } } ``` 在 `pushMessage` 方法中,可以遍历所有连接的客户端会话,向它们发送消息。 最后,在需要推送数据的地方,注入 `MyWebSocketHandler`,调用 `pushMessage` 方法即可: ``` @Autowired private MyWebSocketHandler webSocketHandler; public void sendData() { // 处理数据 webSocketHandler.pushMessage(data); } ``` 至此,我们就成功地在 Spring Boot 2.0 中整合了 WebSocket,并实现了服务器主动向前端推送数据的功能。 ### 回答3: 随着现代web应用的流行,实时数据交换变得越来越重要,而websocket作为实时双向通信的技术,成为了重要的实时数据传输协议。Spring Boot2.0整合websocket可以让我们通过服务器主动向前端推送数据,实现实时数据交换,满足现代web应用的高实时性需求。 WebSocket是一种基于HTTP协议的双向通信协议,在通信过程中,浏览器和服务器相互发送数据,实现实时数据交互。Spring Boot2.0已经内置了对WebSocket协议的支持,通过使用Spring WebSocket API可以很容易地配置WebSocket端点和处理程序。 服务器端可通过定义一个WebSocketConfig的配置类,配置WebSocket的Endpoint和Handler,并注册到拦截器链中,如下所示: @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(webSocketHandler(), "/ws").addInterceptors(new HttpSessionHandshakeInterceptor()); } @Bean public WebSocketHandler webSocketHandler() { return new MyWebSocketHandler(); } } 在MyWebSocketHandler中,通过实现WebSocketHandler接口的handleMessage方法,可以处理客户端发送来的消息。如下所示: public class MyWebSocketHandler implements WebSocketHandler { @Override public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception { //处理客户端发送来的消息 } } 服务器端推送消息到前端,可以通过WebSocketSession的sendMessage方法实现,如下所示: session.sendMessage(new TextMessage("Hello,World!")); 客户端需要使用WebSocket API接收服务器推送来的数据,并处理数据,如下所示: var socket = new WebSocket("ws://localhost:8080/ws"); socket.onmessage = function(event) { //处理服务器推送过来的数据 }; 综上所述,Spring Boot2.0整合websocket可以通过配置WebSocket的Endpoint和Handler,在服务器端主动向前端推送数据,实现实时数据交换。对于现代web应用的高实时性需求,该技术具有重要意义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值