You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot WebSocket(STOMP)连接localhost:8080/ws失败求助

Troubleshooting WebSocket Connection Drops in Your Custom App

Hey there! Let's dig into why your WebSocket connection keeps failing with that "Whoops! Lost connection to http://localhost:8080/ws" error. Based on your scenario and the code snippet you shared, here are the most likely issues and fixes to try:

  • Check your endpoint registration in the WebSocket config
    It looks like you might be missing the proper endpoint setup in your WebSocketConfiguration class. Make sure you've overridden the registerStompEndpoints method to map your /ws path and enable SockJS fallback. Here's what that should look like:

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // Register the /ws endpoint and enable SockJS for browsers that don't support native WebSockets
        registry.addEndpoint("/ws").withSockJS();
        // If your frontend is on a different origin (port/domain), add CORS allowed origins:
        // registry.addEndpoint("/ws").setAllowedOrigins("http://localhost:3000").withSockJS();
    }
    

    Double-check that your frontend is connecting to the exact same path (e.g., new SockJS('/ws') in your client code).

  • Verify your message broker configuration
    A misconfigured message broker can cause connections to drop right after handshake. Ensure you've overridden configureMessageBroker to set up the correct prefixes:

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        // Prefix for messages sent from frontend to your backend controllers
        config.setApplicationDestinationPrefixes("/app");
        // Enable an in-memory broker for topics/queues (adjust prefixes to match your use case)
        config.enableSimpleBroker("/topic", "/queue");
    }
    

    Without this, the broker won't route messages properly, leading to unexpected disconnections.

  • Check your dependencies and Spring version
    Make sure you have the correct WebSocket starter dependency in your project. For Spring Boot, that's:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>
    

    Version mismatches between Spring modules can also cause connection issues—ensure all your Spring dependencies are using a consistent version.

  • Inspect detailed error logs
    The "Lost connection" message is pretty generic. Check your browser's developer tools (Network tab, look for the WebSocket request) and your backend logs for more details:

    • Is the initial handshake returning a 404? That means the /ws endpoint isn't registered correctly.
    • Are there CORS errors? You'll see those in the browser console, which means you need to add allowed origins to your endpoint.
    • Does the backend log show any exceptions during connection setup? That could point to a misconfiguration in your message broker or handler beans.
  • Ensure no firewall/proxy is blocking the connection
    If you're running behind a proxy or local firewall, it might be interrupting the WebSocket connection. Try testing on a clean environment without proxies, or ensure your proxy is configured to support WebSocket (which uses the Upgrade header).

内容的提问来源于stack exchange,提问作者appzone_oto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:00:03