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);
        }
    }
}

加上这两个文件,就可以了

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐