ai问答改成websocket
This commit is contained in:
@@ -17,7 +17,6 @@ import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
|
||||
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -58,6 +57,7 @@ public class ShiroConfig {
|
||||
filterMap.put("/common/apkConfig/getApkUrl","anon");//获取apk下载地址
|
||||
|
||||
filterMap.put("/common/sysFeedback/addSysFeedback","anon");//问题反馈-密码问题
|
||||
filterMap.put("/websocket/**","anon");
|
||||
|
||||
filterMap.put("/oss/**","anon");
|
||||
filterMap.put("/image/**","anon");
|
||||
|
||||
64
src/main/java/com/peanut/config/WebSocket.java
Normal file
64
src/main/java/com/peanut/config/WebSocket.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package com.peanut.config;
|
||||
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import javax.websocket.OnClose;
|
||||
import javax.websocket.OnMessage;
|
||||
import javax.websocket.OnOpen;
|
||||
import javax.websocket.Session;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
|
||||
/**
|
||||
*WebSocket的服务端
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@ServerEndpoint("/websocket")
|
||||
public class WebSocket {
|
||||
|
||||
|
||||
private Session session;
|
||||
|
||||
private static CopyOnWriteArraySet<WebSocket> webSocketSet = new CopyOnWriteArraySet<>();
|
||||
|
||||
@OnOpen
|
||||
public void onOpen(Session session) {
|
||||
this.session = session;
|
||||
webSocketSet.add(this);
|
||||
log.info("【websocket消息】有新的连接, 总数:{}", webSocketSet.size());
|
||||
}
|
||||
|
||||
//前端关闭时一个websocket时
|
||||
@OnClose
|
||||
public void onClose() {
|
||||
webSocketSet.remove(this);
|
||||
log.info("【websocket消息】连接断开, 总数:{}", webSocketSet.size());
|
||||
}
|
||||
|
||||
//前端向后端发送消息
|
||||
@OnMessage
|
||||
public void onMessage(String message) {
|
||||
log.info("【websocket消息】收到客户端发来的消息:{}", message);
|
||||
}
|
||||
|
||||
//新增一个方法用于主动向客户端发送消息
|
||||
public static void sendMessage(String message) {
|
||||
for (WebSocket webSocket: webSocketSet) {
|
||||
log.info("【websocket消息】, message={}", message);
|
||||
try {
|
||||
webSocket.session.getBasicRemote().sendText(message);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
16
src/main/java/com/peanut/config/WebSocketConfig.java
Normal file
16
src/main/java/com/peanut/config/WebSocketConfig.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.peanut.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
||||
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSocket
|
||||
public class WebSocketConfig{
|
||||
@Bean
|
||||
public ServerEndpointExporter serverEndpointExporter() {
|
||||
return new ServerEndpointExporter();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user