46 lines
1.2 KiB
Java
46 lines
1.2 KiB
Java
package com.ruoyi.common.config;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import javax.websocket.*;
|
|
import javax.websocket.server.ServerEndpoint;
|
|
import java.io.IOException;
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
|
|
@ServerEndpoint(value="/websocket",configurator = WebSocketConfig.class)
|
|
@Component
|
|
public class RadarStatusWebSocket {
|
|
|
|
private static Set<Session> sessions = new HashSet<>();
|
|
|
|
@OnOpen
|
|
public void onOpen(Session session) {
|
|
sessions.add(session);
|
|
}
|
|
|
|
@OnClose
|
|
public void onClose(Session session) {
|
|
sessions.remove(session);
|
|
}
|
|
|
|
public static void sendToAll(String message) {
|
|
for (Session session : sessions) {
|
|
try {
|
|
session.getBasicRemote().sendText(message);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void sendToAll(Object dataResponse) {
|
|
for (Session session : sessions) {
|
|
try {
|
|
session.getBasicRemote().sendObject(dataResponse);
|
|
} catch (IOException | EncodeException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
} |