websocket服务端,运行后始终无法连接的解决方案

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> 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 sessionList = sessionsMap.get(sid);
        if (sessionList != null) {
            sessionList.remove(session);
            if (sessionList.isEmpty()) {
                sessionsMap.remove(sid);
            }
        }
    }

    public static List 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);
        }
    }
}

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

你可能感兴趣的:(websocket,网络协议,网络)