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

import com.gyee.wisdom.dataadapter.websocket.session.WisdomStompSession;
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.SessionConnectedEvent;

import java.util.Date;

@Component
public class SessionConnectedEventListener implements
        ApplicationListener<SessionConnectedEvent> {

    @Autowired
    private StompSessionService stompSessionService;

    @Override
    public void onApplicationEvent(SessionConnectedEvent event) {
        //System.out.println("SessionConnectedEvent  rised!");
        //System.out.println("message: " + event.getMessage());
        StompHeaderAccessor accessor = StompHeaderAccessor.wrap(event.getMessage());
        WisdomStompSession session = new WisdomStompSession();
        session.setSessionId(accessor.getSessionId());
        session.setClientIp(accessor.getHost());
        session.setCreateTime(new Date());

        stompSessionService.addSession(session);
        System.out.print("-[+]-");
    }

}