package com.its.api.websocket;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

@Slf4j
@Data
public class ItsWebSocketSession {

    private ItsWebSocketHandler itsWebsocketHandler;
    private WebSocketSession session;
    private String groupId;

    public ItsWebSocketSession(ItsWebSocketHandler itsWebsocketHandler, org.springframework.web.socket.WebSocketSession session) {
        this.itsWebsocketHandler = itsWebsocketHandler;
        this.session = session;
    }

    public void sendMessage(String command, TextMessage message) {

        if (this.session != null && this.session.isOpen()) {
            synchronized (this.session) {
                try {
                    this.session.sendMessage(message);
                } catch (Exception e) {
                    log.error("sendMessage: nodeId: {}, session: {}, {}", command, session, e.getMessage());
                }
            }
        }
    }
}
