Browse Source

external services fix 2

master
gsd 1 year ago
parent
commit
115a5bc650
  1. 4
      src/main/java/app/services/ExternalServices.java
  2. 14
      src/main/java/app/websocket/handlers/ExternalServicesHandler.java

4
src/main/java/app/services/ExternalServices.java

@ -51,4 +51,8 @@ public class ExternalServices {
if (name == null || name.isEmpty()) return; if (name == null || name.isEmpty()) return;
services.put(name, Instant.now().getEpochSecond()); services.put(name, Instant.now().getEpochSecond());
} }
public void inactive(String name) {
services.put(name, 0L);
}
} }

14
src/main/java/app/websocket/handlers/ExternalServicesHandler.java

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

Loading…
Cancel
Save