package com.ruoyi.cai.ws.util;

import com.ruoyi.cai.ws.constant.HangUpEnums;
import com.ruoyi.cai.ws.dto.WsRMsgGen;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;

@Slf4j
public class WsExceptionUtil {
    public static void throwException(String sessionKey, String message, HangUpEnums hangUpType, Long roomId){
        if(hangUpType == null){
            hangUpType = HangUpEnums.OTHER;
        }
        RoomWebSocketUtil.sendSendMessage(sessionKey, WsRMsgGen.hangup(message,roomId,hangUpType.getCode()));
        boolean close = WebSocketUtils.close(sessionKey);
        if(!close){
            log.warn("ws连接时发现异常：{}，sessionKey {} roomid {}", message, sessionKey, roomId);
        }
    }

    public static void throwException(WebSocketSession sessionKey, String message, HangUpEnums hangUpType, Long roomId){
        if(hangUpType == null){
            hangUpType = HangUpEnums.OTHER;
        }
        RoomWebSocketUtil.sendSendMessage(sessionKey, WsRMsgGen.hangup(message,roomId,hangUpType.getCode()));
        boolean close = WebSocketUtils.close(sessionKey);
        if(!close){
            log.warn("ws连接时发现异常：{}，sessionKey {} roomid {}", message, sessionKey, roomId);
        }
    }

    public static void throwExceptionFast(WebSocketSession session,String message){
        RoomWebSocketUtil.sendSendMessage(session, WsRMsgGen.hangup(message, null, HangUpEnums.OTHER.getCode()));
        try {
            session.close();
        } catch (IOException e) {
            log.error("快速关闭ws 失败",e);
        }
    }
}
