当前位置: 首页 > news >正文

vue3里面使用Socketjs之后打包完访问的时候报socketStore-BmspPEpN.js:1 WebSocket connection to

问题原因与解决方案

出现 404 Not Found 错误(请求 iframe.html 失败)的根本原因是 SockJS 所需的静态资源文件未正确部署到服务器,或 代理配置未正确处理 SockJS 路径。以下是逐步排查和解决方案:


1. 确认 SockJS 的静态资源是否部署

问题原因

SockJS 在建立连接时,会尝试加载 iframe.htmliframe.xhtml 等文件。这些文件需要存在于服务器的静态资源目录中,路径需与 WebSocket 端点一致(例如 /webrtcSocketConnect/)。

解决方案
  1. 手动添加 iframe.html 到 Spring Boot 静态目录
    • 创建目录:src/main/resources/static/webrtcSocketConnect/
    • 从 SockJS 官方库 下载 iframe.html,并复制到上述目录。
    文件结构示例

    src/main/resources/static/
      └─ webrtcSocketConnect/
          ├─ iframe.html
          └─ other-sockjs-files...
    
  2. 验证文件是否被打包
    • 执行 mvn clean package 后,解压生成的 JAR 文件,检查路径 BOOT-INF/classes/static/webrtcSocketConnect/iframe.html 是否存在。

其实就是得要SpringBoot的框架里面有个代理Socketjs的包里面就有上面的,代理到里面就可以了,我这里的主要的原因是我把打包的包放到了resource下面的静态资源文件夹里面但是还是要代理webrtcSocketConnect到SpringBoot里面就可以了


2. 检查 Nginx 代理配置

问题原因

Nginx 未正确代理 /webrtcSocketConnect 路径,或未处理 WebSocket 的协议升级请求。

解决方案

修正 Nginx 配置,确保代理路径和协议升级头正确:

server {
    listen 443 ssl;
    server_name 192.168.199.191;

    ssl_certificate     D:/mkcert/192.168.199.191.pem;
    ssl_certificate_key D:/mkcert/192.168.199.191-key.pem;

    # 代理 SockJS 请求到后端
    location /webrtcSocketConnect {
        proxy_pass http://localhost:8080;  # 后端 Spring Boot 端口
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # 解决跨域
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
    }

    # 其他路径代理(可选)
    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
    }
}

3. 检查 Spring Boot 的 WebSocket 配置

问题原因

后端未启用 SockJS 支持,或未正确配置 WebSocket 端点。

解决方案

确保 Spring Boot 正确配置 WebSocket 并允许跨域:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 注册 WebSocket 端点并启用 SockJS
        registry.addEndpoint("/webrtcSocketConnect")
                .setAllowedOriginPatterns("*")  // 允许所有来源
                .withSockJS();  // 启用 SockJS 支持
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic");  // 消息代理前缀
        registry.setApplicationDestinationPrefixes("/app");  // 应用目标前缀
    }
}

4. 检查 Spring Security 配置

问题原因

Spring Security 拦截了 WebSocket 相关请求(如 iframe.html 或 WebSocket 握手请求)。

解决方案

在安全配置中放行 WebSocket 路径:

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .csrf().disable()  // 关闭 CSRF
            .authorizeRequests()
                .antMatchers("/webrtcSocketConnect/**").permitAll()  // 放行 WebSocket 路径
                .anyRequest().authenticated();
        return http.build();
    }
}

5. 验证并测试

步骤
  1. 直接访问 iframe.html
    • 运行 Spring Boot 后,访问 http://localhost:8080/webrtcSocketConnect/iframe.html,确认返回 200 OK。
    • 通过 Nginx 代理访问 https://192.168.199.191/webrtcSocketConnect/iframe.html,同样应返回 200。

  2. 检查 WebSocket 连接
    • 打开浏览器控制台(F12 → Network),观察 WebSocket 握手是否成功。
    • 查看 Nginx 日志(logs/error.log)和 Spring Boot 日志,确认无错误。


常见问题总结

错误现象原因解决方案
404 Not Found静态资源未部署手动添加 iframe.html 到静态目录
WebSocket 握手失败Nginx 未处理协议升级添加 UpgradeConnection
403 ForbiddenSpring Security 拦截放行 /webrtcSocketConnect/** 路径
跨域错误(CORS)后端未允许跨域配置 setAllowedOriginPatterns("*")

完成以上步骤后,404 Not Found 和 WebSocket 连接失败问题应被解决。如果仍有问题,请提供 Nginx 和 Spring Boot 的日志片段。

相关文章:

  • HarmonyOS Next应用架构设计与模块化开发详解
  • 数据:$UPC 上涨突破 5.8 USDT,近7日总涨幅达 73.13%
  • 常见中间件漏洞攻略-Tomcat篇
  • Spring Boot定时任务设置与实现
  • 5.3 位运算专题:LeetCode 371. 两整数之和
  • 区块链驱动金融第十章——走进另类币与加密货币生态系统:比特币之外的广阔天地
  • 知识库外挂 vs 大脑全开:RAG与纯生成式模型(如GPT)的终极Battle
  • vue判断组件是否有传入的slot,有就渲染slot,没有就渲染内部节点默认内容
  • MATLAB—从入门到精通的第四天:函数、绘图与数学魔法
  • 【Python机器学习】3.5. 决策树实战:基于Iris数据集
  • 使用LLama-Factory的简易教程(Llama3微调案例+详细步骤)
  • 【RabbitMQ高级特性】消息确认机制、持久化、发送方确认、TTL和死信队列
  • 腾讯云大模型知识引擎×DeepSeek | 企业应用快速接入手册
  • 【Redis实战专题】「技术提升系列」​RedisJSON核心机制与实战应用解析(入门基础篇)
  • Spring MVC配置
  • Jackson使用ArrayNode对象实现JSON列表数据(二):增、删、改、查
  • Redis 发布订阅
  • GZCTF平台搭建及题目上传
  • 基于简单神经网络的线性回归
  • 【Vue3入门1】01-Vue3的基础 + ref reactive
  • 十四届全国人大常委会第十五次会议继续审议民营经济促进法草案
  • 商务部:4月份以来的出口总体延续平稳增长态势
  • 没有雷军的车展:老外扎堆,萌车、机器狗谁更抢镜?| 湃客Talk
  • 起底网红热敷贴“苗古金贴”:“传承人”系AI生成,“千年秘方”实为贴牌货
  • 巴达玛·利斯瓦达恭当选世界羽联主席,张军任理事会理事
  • 大家聊中国式现代化|邓智团:践行人民城市理念,开创人民城市建设新局面