WebSocket连接404-bug修复

该博客探讨了WebSocket在线上部署时遇到的404错误,主要问题在于前端与后端的通信连接。解决方案包括正确配置nginx以支持WebSocket,检查后端服务类上的注解确保被Spring容器正确识别,以及在Spring Boot应用中注册ServerEndpointExporter以启用WebSocket服务。这些步骤对于确保WebSocket在线上环境的正常运行至关重要。
摘要由CSDN通过智能技术生成

背景:
前后端使用webSocket通信,线下测试没问题,部署线上不能正常使用。

问题1:连接时404,找不到后端onOpen路径

方案1:
nginx配置:

    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

    server {
        location / {
            proxy_pass   http://127.0.0.1:7001;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
        }
    }

方案2:
查看你的socket服务类上有没有项相关注解@Component或者@RestController
这俩个随便一个都可以,否则spring无法发现注册bean

@ServerEndpoint("/{tenantName}/socket/{sessionId}")
@Component
@Slf4j
public class WebSocketServer {

}

方案3:
使用springboot 容器需要注册ServerEndpointExporter

@Configuration
public class WebSocketConfig {
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值