package com.bb.config;

import java.util.HashSet; 
import java.util.Set; 
import org.apache.commons.lang3.StringUtils; 
import org.springframework.messaging.Message; 
import org.springframework.messaging.MessageChannel; 
import org.springframework.messaging.support.ChannelInterceptor; // 인터페이스로 변경
import org.springframework.stereotype.Component;

@Component
public class WebSocketInterceptor implements ChannelInterceptor { // implements로 변경
    Set<String> sessionSet = new HashSet<>(); 
    
    @Override 
    public Message<?> preSend(Message<?> message, MessageChannel channel) { 
        String simpMessageType = String.valueOf(message.getHeaders().get("simpMessageType")); 
        
        if(StringUtils.equals(simpMessageType, "CONNECT")) { 
            String simpSessionId = String.valueOf(message.getHeaders().get("simpSessionId")); 
            sessionSet.add(simpSessionId); 
        } else if(StringUtils.equals(simpMessageType, "DISCONNECT")) { 
            String simpSessionId = String.valueOf(message.getHeaders().get("simpSessionId")); 
            sessionSet.remove(simpSessionId);
        } 
        
        // int uniqueJoinSessionCount = sessionSet.size(); 
        
        // 인터페이스에는 super.preSend()가 없으므로 message를 그대로 리턴합니다.
        return message; 
    } 
}