//package com.mcwl.communityCenter.webSocket;
//
//import com.mcwl.common.utils.SecurityUtils;
//import com.mcwl.communityCenter.service.HumanService;
//import lombok.NoArgsConstructor;
//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.AbstractWebSocketHandler;
//import reactor.core.Disposable;
//
//import javax.websocket.server.ServerEndpoint;
//import java.io.IOException;
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//
//@ServerEndpoint("/chat/human")
//@NoArgsConstructor
//public class HumanWebSocket extends AbstractWebSocketHandler {
//
//    @Autowired
//    private HumanService humanService;
//
//    // 构造函数注入服务...
//
//    @Override
//    public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
//        String userId = session.getId();
//        String userMessage = message.getPayload();
//
//        humanService.handleHumanMessage(userId, userMessage);
//    }
//
//    @Override
//    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
//        super.afterConnectionEstablished(session);
//        humanService.transferToHuman(SecurityUtils.getUserId().toString(), session);
//        System.out.println("客服：" + SecurityUtils.getUsername() + " 已上线");
//    }
//
//
//    // 线程安全的发送方法
//    private void sendText(WebSocketSession session, String text) {
//        try {
//            if (session.isOpen()) {
//                synchronized (session) { // WebSocketSession 非线程安全
//                    session.sendMessage(new TextMessage(text));
//                }
//            }
//        } catch (IOException e) {
//            System.out.println("WebSocket 发送失败: " + e.getMessage());
//        }
//    }
//}