82 lines
2.9 KiB
Java
82 lines
2.9 KiB
Java
//package com.guwan.backend.websocket;
|
|
//
|
|
//import com.alibaba.fastjson.JSON;
|
|
//import com.guwan.backend.entity.LiveMessage;
|
|
//import lombok.extern.slf4j.Slf4j;
|
|
//import org.springframework.stereotype.Component;
|
|
//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;
|
|
//
|
|
//import java.io.IOException;
|
|
//import java.util.Map;
|
|
//import java.util.Set;
|
|
//import java.util.concurrent.ConcurrentHashMap;
|
|
//import java.util.concurrent.ConcurrentHashSet;
|
|
//
|
|
//@Slf4j
|
|
//@Component
|
|
//public class LiveWebSocketHandler extends TextWebSocketHandler {
|
|
//
|
|
// private static final Map<String, Set<WebSocketSession>> roomSessions = new ConcurrentHashMap<>();
|
|
// private static final Map<String, WebSocketSession> userSessions = new ConcurrentHashMap<>();
|
|
//
|
|
// @Override
|
|
// public void afterConnectionEstablished(WebSocketSession session) {
|
|
// String roomId = getRoomId(session);
|
|
// String userId = getUserId(session);
|
|
//
|
|
// // 加入房间
|
|
// roomSessions.computeIfAbsent(roomId, k -> new ConcurrentHashSet<>()).add(session);
|
|
// userSessions.put(userId, session);
|
|
//
|
|
// // 广播进入消息
|
|
// broadcastMessage(roomId, createEnterMessage(userId));
|
|
// }
|
|
//
|
|
// @Override
|
|
// public void handleTextMessage(WebSocketSession session, TextMessage message) {
|
|
// String roomId = getRoomId(session);
|
|
// LiveMessage liveMessage = JSON.parseObject(message.getPayload(), LiveMessage.class);
|
|
//
|
|
// // 处理不同类型的消息
|
|
// switch (liveMessage.getType()) {
|
|
// case "CHAT":
|
|
// broadcastMessage(roomId, message);
|
|
// break;
|
|
// case "GIFT":
|
|
// handleGiftMessage(roomId, liveMessage);
|
|
// break;
|
|
// case "LIKE":
|
|
// handleLikeMessage(roomId, liveMessage);
|
|
// break;
|
|
// }
|
|
// }
|
|
//
|
|
// @Override
|
|
// public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
|
|
// String roomId = getRoomId(session);
|
|
// String userId = getUserId(session);
|
|
//
|
|
// // 离开房间
|
|
// roomSessions.get(roomId).remove(session);
|
|
// userSessions.remove(userId);
|
|
//
|
|
// // 广播离开消息
|
|
// broadcastMessage(roomId, createLeaveMessage(userId));
|
|
// }
|
|
//
|
|
// private void broadcastMessage(String roomId, TextMessage message) {
|
|
// Set<WebSocketSession> sessions = roomSessions.get(roomId);
|
|
// if (sessions != null) {
|
|
// sessions.forEach(session -> {
|
|
// try {
|
|
// session.sendMessage(message);
|
|
// } catch (IOException e) {
|
|
// log.error("发送消息失败", e);
|
|
// }
|
|
// });
|
|
// }
|
|
// }
|
|
//} |