账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    springboot websocket 发布服务器后 报错
    54
    0

    问题描述

    springboot中使用了websocket向前端vue发送消息,本地调试正常。
    发布到服务器centos后,springboot部署jar包,端口9001;
    vue放在nginx下,websocket连接改为服务器外网域名或IP,如下

    this.websock = new WebSocket(
        'ws://' + window.location.hostname + ':9001/websocket/spzs'
      )

    websocket连接变得不稳定,一会报错一会正常,报错为,
    Error during WebSocket handshake: Incorrect 'Sec-WebSocket-Accept' header value

    问题出现的环境背景及自己尝试过哪些方法

    百度查询后,估计应该是后端需要返回一个Sec-WebSocket-Accept头,处理前端升级http请求为websocket时发过来的密钥

    请问有没有大神知道这个该怎么做?没有经验,谢谢

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 得过且过丶 普通会员 1楼

      在Spring Boot中,WebSocket服务器需要连接到Web服务器进行通信。当连接失败时,通常会抛出一个异常。你需要查看具体的异常信息,以便定位问题。

      在错误信息中,通常会包含服务器地址、错误代码、错误消息等信息。如果你的错误信息没有提供足够的信息,可能需要搜索相关的社区或论坛,或者在网上搜索相关的代码。

      以下是一个简单的示例,展示了如何在Spring Boot中发送WebSocket消息:

      ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.client.WebClient; import org.springframework.web.bind.annotation.server.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;

      @SpringBootApplication public class WebSocketServerApplication {

      public static void main(String[] args) {
          SpringApplication.run(WebSocketServerApplication.class, args);
      }
      
      @GetMapping("/ws")
      public String sendWebSocketMessage() {
          // 创建一个WebSocket客户端
          WebClient client = WebClient.create();
      
          // 创建一个WebSocket连接
          WebSocketConnection connection = client.createWebSocketConnection("ws://localhost:8080");
      
          // 发送一个消息
          String message = "Hello, WebSocket!";
          connection.send(message);
      
          // 关闭WebSocket连接
          connection.close();
      
          return "WebSocket message sent successfully!";
      }
      

      } ```

      在这个示例中,我们创建了一个WebSocket客户端和WebSocket连接。然后,我们发送了一个消息,并关闭了连接。注意,你需要将"ws://localhost:8080"替换为你自己的WebSocket服务器地址。

    更多回答
    网站公告
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部