package com.gyee.wisdom.dataadapter.websocket.listener;

import com.gyee.wisdom.dataadapter.websocket.session.StompSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.messaging.SessionDisconnectEvent;

@Component
public class SessionDisconnectEventListener implements
        ApplicationListener<SessionDisconnectEvent> {

    @Autowired
    private StompSessionService stompSessionService;

    @Override
    public void onApplicationEvent(SessionDisconnectEvent event) {
        System.out.println("SessionDisconnectEvent  rised!");
        System.out.println("message: " + event.getMessage());

        StompHeaderAccessor accessor = StompHeaderAccessor.wrap(event.getMessage());
        String sessionId = accessor.getSessionId();
        stompSessionService.removeSession(sessionId);


    }

}