|
|
@ -15,13 +15,15 @@ import org.springframework.web.socket.WebSocketSession; |
|
|
|
import org.springframework.web.socket.handler.TextWebSocketHandler; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
@Component |
|
|
|
public class ExternalServicesHandler extends TextWebSocketHandler implements BaseWebsocketHandler { |
|
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(getClass()); |
|
|
|
private ObjectMapper objectMapper = new ObjectMapper(); |
|
|
|
|
|
|
|
private final Map<String, WebSocketSession> activeSessions = new HashMap<>(); |
|
|
|
private final Map<String, String> nameSessions = new HashMap<>(); |
|
|
|
private ExternalServices externalServices; |
|
|
|
|
|
|
|
@Autowired |
|
|
@ -40,6 +42,7 @@ public class ExternalServicesHandler extends TextWebSocketHandler implements Bas |
|
|
|
super.afterConnectionEstablished(session); |
|
|
|
if (session.isOpen()) { |
|
|
|
logger.info("Session {} open", session.getId()); |
|
|
|
activeSessions.put(session.getId(), session); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -55,12 +58,21 @@ public class ExternalServicesHandler extends TextWebSocketHandler implements Bas |
|
|
|
return; |
|
|
|
} |
|
|
|
String service_name = (String) payload.get("name"); |
|
|
|
if (service_name == null || service_name.isEmpty()) return; |
|
|
|
nameSessions.put(session.getId(), service_name); |
|
|
|
externalServices.update(service_name); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { |
|
|
|
logger.info("Session {} close", session.getId()); |
|
|
|
if (activeSessions.containsKey(session.getId())) { |
|
|
|
activeSessions.remove(session.getId()); |
|
|
|
} |
|
|
|
if (nameSessions.containsKey(session.getId())) { |
|
|
|
externalServices.inactive(nameSessions.get(session.getId())); |
|
|
|
nameSessions.remove(session.getId()); |
|
|
|
} |
|
|
|
super.afterConnectionClosed(session, status); |
|
|
|
} |
|
|
|
} |
|
|
|