fuliqi
2024-07-05 1ba9cdfe3bb5ca3ab4393343776c984a1dcd6302
message命令换成command,websocket手动取bean
7个文件已修改
36 ■■■■■ 已修改文件
src/main/java/com/ycl/jxkg/JxkgApplication.java 5 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ycl/jxkg/config/CaffeineConfig.java 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ycl/jxkg/domain/query/WebSocketQuery.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ycl/jxkg/enums/WebsocketCommendEnum.java 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ycl/jxkg/server/WebsocketServer.java 15 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ycl/jxkg/service/impl/EducationResourceServiceImpl.java 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ycl/jxkg/JxkgApplication.java
@@ -1,9 +1,11 @@
package com.ycl.jxkg;
import com.ycl.jxkg.config.property.SystemConfig;
import com.ycl.jxkg.server.WebsocketServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@@ -25,6 +27,7 @@
     * @param args the input arguments
     */
    public static void main(String[] args) {
        SpringApplication.run(JxkgApplication.class, args);
        ConfigurableApplicationContext context = SpringApplication.run(JxkgApplication.class, args);
        WebsocketServer.setApplicationContext(context);
    }
}
src/main/java/com/ycl/jxkg/config/CaffeineConfig.java
@@ -2,12 +2,14 @@
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
@Configuration
@EnableCaching
public class CaffeineConfig {
    @Bean
src/main/java/com/ycl/jxkg/domain/query/WebSocketQuery.java
@@ -4,6 +4,6 @@
@Data
public class WebSocketQuery {
    private String commend;
    private String command;
    private Integer id;
}
src/main/java/com/ycl/jxkg/enums/WebsocketCommendEnum.java
@@ -17,12 +17,12 @@
    RECORD_STUDY_TIME("recordStudyTime", "记录学习时间"),
    ;
    private final String commend;
    private final String command;
    private final String desc;
    WebsocketCommendEnum(String commend, String desc) {
        this.commend = commend;
    WebsocketCommendEnum(String command, String desc) {
        this.command = command;
        this.desc = desc;
    }
}
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);
src/main/java/com/ycl/jxkg/service/impl/EducationResourceServiceImpl.java
@@ -15,6 +15,7 @@
import com.ycl.jxkg.mapper.SubjectMapper;
import com.ycl.jxkg.service.EducationResourceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -31,6 +32,7 @@
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class EducationResourceServiceImpl implements EducationResourceService {
    private final EducationResourceMapper mapper;
src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java
@@ -885,7 +885,7 @@
            throw new RuntimeException("该学员不在线,无法执行该操作");
        }
        WebsocketDataVO websocket = new WebsocketDataVO();
        websocket.setCommend(WebsocketCommendEnum.DELAYED.getCommend());
        websocket.setCommend(WebsocketCommendEnum.DELAYED.getCommand());
        BigDecimal sed = BigDecimal.valueOf(60).multiply(form.getAddTimeM());
        form.setAddTimeM(sed);
        websocket.setData(form);
@@ -900,7 +900,7 @@
            throw new RuntimeException("该学员不在线,无法执行该操作");
        }
        WebsocketDataVO websocket = new WebsocketDataVO();
        websocket.setCommend(WebsocketCommendEnum.FORCE_SUBMIT.getCommend());
        websocket.setCommend(WebsocketCommendEnum.FORCE_SUBMIT.getCommand());
        websocket.setData(form);
        // 发送websocket消息
        websocketServer.sendOneMessage(form.getUserId(), JSON.toJSONString(form));