xiangpei
2024-07-11 5073a245f53fd5ca936e779be8c6b9b19d42f67d
src/main/java/com/ycl/jxkg/server/WebsocketServer.java
@@ -8,6 +8,7 @@
import com.ycl.jxkg.service.EducationResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpSession;
@@ -32,8 +33,12 @@
@ServerEndpoint("/websocket/{userId}")
public class WebsocketServer {
    @Autowired
    private EducationResourceService educationResourceService;
    private static ApplicationContext applicationContext;
    public static void setApplicationContext(ApplicationContext context) {
        applicationContext = context;
    }
    /**
     * 线程安全的无序的集合
     */
@@ -68,9 +73,11 @@
    @OnMessage
    public void onMessage(String message) {
        WebSocketQuery webSocketQuery = JSONObject.parseObject(message, WebSocketQuery.class);
        String commend = webSocketQuery.getCommend();
        String command = webSocketQuery.getCommand();
        Integer userId = webSocketQuery.getId();
        if(WebsocketCommendEnum.RECORD_STUDY_TIME.getCommend().equals(commend)){
        if(WebsocketCommendEnum.RECORD_STUDY_TIME.getCommand().equals(command)){
            log.info("存消息");
            EducationResourceService educationResourceService = applicationContext.getBean(EducationResourceService.class);
            educationResourceService.recordTime(userId);
        }
        log.info("【WebSocket消息】收到客户端消息:" + message);