wu
2023-09-13 11b9b5a2cfd5b23e80ce6a3be3f9b341a2cd1be0
springboot-vue3/src/main/java/com/example/springboot/component/WebSocketServer.java
@@ -4,6 +4,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -16,8 +17,8 @@
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
@@ -25,6 +26,17 @@
@ServerEndpoint(value = "/api/talk/{username}")
@Component
public class WebSocketServer {
    // @Autowired
    // HomeMapper homeMapper;
    static ConfigurableApplicationContext applicationContext;
    //解决无法注入mapper问题  //使用方法 homeMapper=WebSocketServer.applicationContext.getBean(HomeMapper.class);
    public static void setApplicationContext(ConfigurableApplicationContext configurableApplicationContext) {
        WebSocketServer.applicationContext = configurableApplicationContext;
    }
   private static final Logger log = LoggerFactory.getLogger(WebSocketServer.class);
    private List<String> messages;
@@ -86,6 +98,7 @@
         jsonObject.set("message", text);
        this.messages.add(text);
         this.sendMessage(jsonObject.toString()); //JSONUtil.toJsonStr(jsonObject)
    }
    @OnError
@@ -122,6 +135,7 @@
    public List<String> getMessages() {
        return messages;
    }
    public void clearMessages() {