|
|
@ -3,6 +3,8 @@ package com.iflytop.sgs.app.ws.server; |
|
|
|
import com.iflytop.sgs.app.model.bo.status.device.DeviceState; |
|
|
|
import com.iflytop.sgs.app.service.device.DeviceStateService; |
|
|
|
import com.iflytop.sgs.common.constant.WebSocketMessageType; |
|
|
|
import jakarta.annotation.PostConstruct; |
|
|
|
import jakarta.annotation.Resource; |
|
|
|
import jakarta.websocket.*; |
|
|
|
import jakarta.websocket.server.ServerEndpoint; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
@ -17,14 +19,23 @@ import java.util.Set; |
|
|
|
@Slf4j |
|
|
|
@ServerEndpoint("/ws") |
|
|
|
@Component |
|
|
|
@RequiredArgsConstructor |
|
|
|
public class WebSocketServer { |
|
|
|
|
|
|
|
private static final Set<Session> sessions = Collections.synchronizedSet(new HashSet<>()); |
|
|
|
private final DeviceStateService deviceStateService; |
|
|
|
private final WebSocketSender webSocketService; |
|
|
|
|
|
|
|
private static DeviceStateService staticDeviceStateService; |
|
|
|
private static WebSocketSender staticWebSocketService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private DeviceStateService deviceStateService; |
|
|
|
@Autowired |
|
|
|
private WebSocketSender webSocketService; |
|
|
|
@PostConstruct |
|
|
|
public void init() { |
|
|
|
staticDeviceStateService = this.deviceStateService; |
|
|
|
staticWebSocketService = this.webSocketService; |
|
|
|
} |
|
|
|
public WebSocketServer() {} |
|
|
|
public static void sendMessageToClients(String message) { |
|
|
|
synchronized (sessions) { |
|
|
|
for (Session session : sessions) { |
|
|
@ -40,7 +51,7 @@ public class WebSocketServer { |
|
|
|
@OnOpen |
|
|
|
public void onOpen(Session session) { |
|
|
|
sessions.add(session); |
|
|
|
webSocketService.push(WebSocketMessageType.STATUS, deviceStateService.getDeviceState()); |
|
|
|
staticWebSocketService.push(WebSocketMessageType.STATUS, staticDeviceStateService.getDeviceState()); |
|
|
|
log.info("新连接加入,sessionId={}", session.getId()); |
|
|
|
} |
|
|
|
|
|
|
|