package com.educoder.bridge.handler;

import com.educoder.bridge.service.JchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

public class WebsshHandler extends TextWebSocketHandler {

    @Autowired
    JchService jchService;

    @Override
    public void afterConnectionEstablished(WebSocketSession wsSession) throws Exception {
        super.afterConnectionEstablished(wsSession);
        jchService.add(wsSession);
    }

    /**
     * 重写handleTextMessage方法，用于处理从websocket接收到的信息
     */
    @Override
    protected void handleTextMessage(WebSocketSession wsSession, TextMessage message) throws Exception {
        jchService.recv(message.getPayload(), wsSession);
        super.handleTextMessage(wsSession, message);
    }


    @Override
    public void afterConnectionClosed(WebSocketSession wsSession, CloseStatus status) throws Exception {
        super.afterConnectionClosed(wsSession, status);
        jchService.closeByWebSocket(wsSession);
    }
}
