Home > Java > javaTutorial > body text

How does SpringBoot integrate WebSocket to enable the backend to send messages to the frontend?

WBOY
Release: 2023-05-11 14:07:12
forward
4214 people have browsed it

1. What is the websocket interface

Use websocket to establish a long connection. The server and the client can communicate with each other. As long as there is data update on the server, it can actively push it to the client.

WebSocket makes data exchange between the client and the server simpler, allowing the server to actively push data to the client. In the WebSocket API, the browser and the server only need to complete a handshake, and a persistent connection can be created directly between the two for bidirectional data transmission.
In the WebSocket API, the browser and the server only need to perform a handshake action, and then a fast channel is formed between the browser and the server. Data can be transmitted directly between the two.

2. Applicable Scenarios

During the business development process, we encounter some asynchronous processing (payment notifications for WeChat payment and Alipay payment) and cross-application messaging.

After the business execution is completed, successful information needs to be delivered to the front end. Under normal circumstances, the front end calls the http/https interface of the back end to obtain data. If the back end wants to actively push messages to the front end, it needs to use WebSocket for communication between the front and back ends.

3. Sample code

3.1. Add pom.xml dependency

<!-- websocket-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
Copy after login

3.2. Create WebSokcet configuration class

@Configuration
public class WebSocketConfig {
    @Bean
    public ServerEndpointExporter serverEndpointExporter(){
        return new ServerEndpointExporter();
    }
}
3.3、创建WebSokcet工具类
 
@ServerEndpoint(value = "/websocket")
@Component
public class WebSocketServer {
    private final static Logger log = LoggerFactory.getLogger(WebSocketServer.class);
 
    //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
    private static int onlineCount = 0;
    //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
    private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>();
 
    //与某个客户端的连接会话,需要通过它来给客户端发送数据
    private Session session;
 
    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
        //加入set中
        webSocketSet.add(this);
        //在线数加1
        addOnlineCount();
        log.info("有新连接加入!当前在线人数为" + getOnlineCount());
        try {
            MsgResponseVo userMsgResponseVo = new MsgResponseVo();
            userMsgResponseVo.setMsg("SUCCESS");
            WebSocketServer.sendInfo(JSON.toJSONString(userMsgResponseVo));
        } catch (IOException e) {
            log.error("websocket IO异常");
        }
    }
 
    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose() {
        //从set中删除
        webSocketSet.remove(this);
        //在线数减1
        subOnlineCount();
        log.info("有一连接关闭!当前在线人数为" + getOnlineCount());
    }
 
    /**
     * 收到客户端消息后调用的方法
     *
     * @param message 客户端发送过来的消息
     */
    @OnMessage
    public void onMessage(String message, Session session) {
        log.info("来自客户端的消息:" + message);
 
        //群发消息
        for (WebSocketServer item : webSocketSet) {
            try {
                item.sendMessage(message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    /**
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {
        log.error("发生错误");
        error.printStackTrace();
    }
 
    public void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);
    }
 
    /**
     * 群发自定义消息
     */
    public static void sendInfo(String message) throws IOException {
        log.info(message);
        for (WebSocketServer item : webSocketSet) {
            try {
                item.sendMessage(message);
            } catch (IOException e) {
                continue;
            }
        }
    }
 
    public static synchronized int getOnlineCount() {
        return onlineCount;
    }
 
    public static synchronized void addOnlineCount() {
        WebSocketServer.onlineCount++;
    }
 
    public static synchronized void subOnlineCount() {
        WebSocketServer.onlineCount--;
    }
}
Copy after login

3.3. Create a test message sending interface

 @GetMapping("/testWebSocket")
    public ApiRestResponse testWebSocket() throws IOException {
        //消息体
        MsgResponseVo technicianMsgResponseVo = new MsgResponseVo();
        technicianMsgResponseVo.setRole("Technician");
        technicianMsgResponseVo.setRoleId(1);
        technicianMsgResponseVo.setMsg("您的订单已取消");
        technicianMsgResponseVo.setMsgStatus("CANCEL_ORDER");
        technicianMsgResponseVo.setOrderNo("test");
        //发送消息
        WebSocketServer.sendInfo(JSON.toJSONString(technicianMsgResponseVo));
        return ApiRestResponse.success();
    }
}
Copy after login

3.4. Test webSocket

Enter the prefix of ws://ip:port/webSocket tool class in the website (ws://127.0.0.1:8080/websocket)

How does SpringBoot integrate WebSocket to enable the backend to send messages to the frontend?

3.5. The front-end uses WebSocket to monitor the back-end WebSocket address, and performs the next step of business processing after receiving the message.

The above is the detailed content of How does SpringBoot integrate WebSocket to enable the backend to send messages to the frontend?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!