javax.websocket.DeploymentException: The HTTP response from the server [404] did not permit the HTTP
解决办法:少两个文件:
WebSocketConfig.java
@Configuration
public class WebSocketConfig {
/**
* 注入一个ServerEndpointExporter,该Bean会自动注册使用@ServerEndpoint注解申明的websocket endpoint
*/
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
和
WebSocketManager.java
@Component
public class WebSocketManager {
private static final Map<String, List<Session>> sessionsMap = new ConcurrentHashMap<>();
public static void addSession(String sid, Session session) {
sessionsMap.computeIfAbsent(sid, k -> new CopyOnWriteArrayList<>()).add(session);
}
public static void removeSession(String sid, Session session) {
List<Session> sessionList = sessionsMap.get(sid);
if (sessionList != null) {
sessionList.remove(session);
if (sessionList.isEmpty()) {
sessionsMap.remove(sid);
}
}
}
public static List<Session> getSessions(String sid) {
return sessionsMap.getOrDefault(sid, Collections.emptyList());
}
public static void sendToAll(String sid, String message) throws IOException {
for (Session session : getSessions(sid)) {
session.getBasicRemote().sendText(message);
}
}
}
加上这两个文件,就可以了