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.
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.
<!-- websocket--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>
@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--; } }
@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(); } }
Enter the prefix of ws://ip:port/webSocket tool class in the website (ws://127.0.0.1:8080/websocket)
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!