feature: [WebSocket/ STOMP]
This commit is contained in:
parent
5d73dc5033
commit
c64f4ae077
|
@ -1,2 +1 @@
|
|||
学习记录 加个表 前端对个接口
|
||||
直播表
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package com.guwan.backend.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
||||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSocketMessageBroker
|
||||
public class StompWebSocketConfig implements WebSocketMessageBrokerConfigurer {
|
||||
|
||||
@Override
|
||||
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
||||
// STOMP 客户端连接入口,路径不要和原始 WebSocket 冲突
|
||||
registry.addEndpoint("/stomp")
|
||||
.setAllowedOriginPatterns("*")
|
||||
.withSockJS(); // 启用 SockJS 兼容
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureMessageBroker(MessageBrokerRegistry registry) {
|
||||
// 客户端订阅路径前缀,topic 表示广播
|
||||
registry.enableSimpleBroker("/topic", "/queue");
|
||||
// 客户端发送消息的路径前缀
|
||||
registry.setApplicationDestinationPrefixes("/live");
|
||||
}
|
||||
}
|
|
@ -30,6 +30,8 @@ public class SecurityConstants {
|
|||
|
||||
"/challenge",
|
||||
"/ws/**",
|
||||
"/stomp/**",
|
||||
|
||||
"/faceTest", "/compareFaces",
|
||||
"/minio/**",
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ import com.guwan.backend.pojo.dto.course.InsertCourseDTO;
|
|||
import com.guwan.backend.pojo.entity.*;
|
||||
import com.guwan.backend.pojo.response.CourseByAdminVO;
|
||||
import com.guwan.backend.pojo.response.CourseCenterVO;
|
||||
import com.guwan.backend.pojo.response.LiveDetailVO;
|
||||
import com.guwan.backend.pojo.response.courseDetail.BaseSectionVO;
|
||||
import com.guwan.backend.pojo.response.courseDetail.ChapterVO;
|
||||
import com.guwan.backend.pojo.response.courseDetail.CourseDetailVO;
|
||||
|
@ -393,9 +394,25 @@ public class CourseController {
|
|||
return SearchResult.success(courseCenterVOList, count);
|
||||
}
|
||||
|
||||
@PostMapping("/testMethod")
|
||||
public void testMethod(@RequestParam(name = "page") Long pageNum){
|
||||
@GetMapping("/live/detail")
|
||||
public Result getLiveDetail(@RequestParam String courseId){
|
||||
|
||||
Course course = courseService.getById(courseId);
|
||||
|
||||
LiveDetailVO liveDetailVO = new LiveDetailVO();
|
||||
liveDetailVO.setId(course.getId());
|
||||
liveDetailVO.setTitle(course.getTitle());
|
||||
String teacherId = course.getTeacherId();
|
||||
liveDetailVO.setHostId(teacherId);
|
||||
Teacher teacher = teacherService.getById(teacherId);
|
||||
liveDetailVO.setHostName(teacher.getName());
|
||||
liveDetailVO.setHostAvatar(teacher.getAvatar());
|
||||
liveDetailVO.setDescription(course.getDescription());
|
||||
liveDetailVO.setStatus("live");
|
||||
liveDetailVO.setViewerCount(0);
|
||||
liveDetailVO.setStreamUrl(course.getLiveUrl());
|
||||
|
||||
return Result.success(liveDetailVO);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
package com.guwan.backend.controller;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.messaging.handler.annotation.DestinationVariable;
|
||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||
import org.springframework.messaging.handler.annotation.Payload;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
@Controller
|
||||
public class StompChatController {
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
// 前端发送到 /live/send/{liveId}
|
||||
@MessageMapping("/send/{liveId}")
|
||||
@SendTo("/topic/live/{liveId}")
|
||||
public String processMessage(@DestinationVariable String liveId, @Payload String message) {
|
||||
try {
|
||||
// 将消息字符串转换为 JSON 对象
|
||||
|
||||
/**
|
||||
* asText() 转为字符串
|
||||
* asInt() 转为整数
|
||||
* asLong() 转为长整数(时间戳等)
|
||||
* asBoolean() 转为布尔值
|
||||
* get("key") 获取字段值,返回 JsonNode
|
||||
*/
|
||||
JsonNode jsonNode = objectMapper.readTree(message);
|
||||
JsonNode node = jsonNode.get("username");
|
||||
String username = null;
|
||||
if (node != null && !node.isNull()) {
|
||||
username = node.asText();
|
||||
}
|
||||
System.out.println(username);
|
||||
System.out.println("Received message: " + jsonNode.toPrettyString());
|
||||
|
||||
// 返回处理后的消息
|
||||
return message;
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error processing message: " + e.getMessage());
|
||||
return message;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.guwan.backend.pojo.response;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class LiveDetailVO {
|
||||
/**
|
||||
* courseId
|
||||
*/
|
||||
private String id;
|
||||
private String title;
|
||||
private String hostName;
|
||||
private String hostId;
|
||||
private String hostAvatar;
|
||||
private Integer viewerCount;
|
||||
private String status;
|
||||
private String description;
|
||||
private String streamUrl;
|
||||
|
||||
}
|
Loading…
Reference in New Issue