From ea5be111f899db0af7356bbee88c07161875cc54 Mon Sep 17 00:00:00 2001 From: whycxzp <glperry@163.com> Date: 星期一, 16 六月 2025 19:12:46 +0800 Subject: [PATCH] 领导层首页-实时告警信息 --- src/main/java/com/whyc/webSocket/LeaderHomeSubModule1Socket.java | 85 ++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 85 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/whyc/webSocket/LeaderHomeSubModule1Socket.java b/src/main/java/com/whyc/webSocket/LeaderHomeSubModule1Socket.java new file mode 100644 index 0000000..8a87fd7 --- /dev/null +++ b/src/main/java/com/whyc/webSocket/LeaderHomeSubModule1Socket.java @@ -0,0 +1,85 @@ +package com.whyc.webSocket; + +import com.whyc.config.WebSocketConfig; +import com.whyc.dto.NameValueIntDto; +import com.whyc.dto.Response; +import com.whyc.pojo.db_user.User; +import com.whyc.service.LeaderHomeService; +import com.whyc.util.JsonUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.servlet.http.HttpSession; +import javax.websocket.*; +import javax.websocket.server.ServerEndpoint; +import java.io.IOException; + +/** + * 棰嗗灞傞椤� 瀛愭ā鍧� + * 鍚屽搧鐗屽悓鏃堕棿 + */ +@Component +@ServerEndpoint(value = "/leaderHome/BattSameTimeSameBrand",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class) +public class LeaderHomeSubModule1Socket { + + private Session session; + + private Thread thread; + + private static HttpSession httpSession; + private static LeaderHomeService leaderHomeService; + + @Autowired + public void setLeaderHomeService(LeaderHomeService leaderHomeService) { + LeaderHomeSubModule1Socket.leaderHomeService = leaderHomeService; + } + + @OnOpen + public void onOpen(Session session, EndpointConfig config){ + this.session = session; + httpSession = (HttpSession) config.getUserProperties().get("httpSession"); + } + + @OnMessage + public void onMessage(Session session, String message) { + //value涓烘姇杩愭椂闂�,骞翠负鍗曚綅 + //name涓哄搧鐗� + NameValueIntDto nameValueDto = JsonUtil.getGson().fromJson(message, NameValueIntDto.class); + User user = (User) httpSession.getAttribute("user"); + Integer userId = user.getId(); + Thread thread = new Thread() { + @Override + public void run() { + try { + while (!currentThread().isInterrupted()) { + Response response = leaderHomeService.getBattPerformanceOfSameTimeSameBrand(userId, nameValueDto); + session.getBasicRemote().sendObject(new Response().set(1, response)); + sleep(4000); + } + } catch (Exception e) { + this.interrupt(); + } + } + }; + thread.start(); + this.thread = thread; + } + + @OnClose + public void onClose(CloseReason closeReason) throws IOException { + System.err.println("closeReason = " + closeReason); + if(session.isOpen()){ + session.close(); + } + } + + @OnError + public void onError(Throwable error) throws IOException { + error.printStackTrace(); + thread.isInterrupted(); + if(session.isOpen()){ + session.close(); + } + } + +} -- Gitblit v1.9.1