ItsWebSocketSession.java 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. package com.its.api.websocket;
  2. import lombok.Data;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.web.socket.TextMessage;
  5. import org.springframework.web.socket.WebSocketSession;
  6. @Slf4j
  7. @Data
  8. public class ItsWebSocketSession {
  9. private ItsWebSocketHandler itsWebsocketHandler;
  10. private WebSocketSession session;
  11. private String groupId;
  12. public ItsWebSocketSession(ItsWebSocketHandler itsWebsocketHandler, org.springframework.web.socket.WebSocketSession session) {
  13. this.itsWebsocketHandler = itsWebsocketHandler;
  14. this.session = session;
  15. }
  16. public void sendMessage(String command, TextMessage message) {
  17. if (this.session != null && this.session.isOpen()) {
  18. synchronized (this.session) {
  19. try {
  20. this.session.sendMessage(message);
  21. } catch (Exception e) {
  22. log.error("sendMessage: nodeId: {}, session: {}, {}", command, session, e.getMessage());
  23. }
  24. }
  25. }
  26. }
  27. }