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

SpringCloud 网关配置websocket

一、nginx

  https://域名.com

    location /websocket/ {
        proxy_pass http://172.1.1.173:8181/;   #内网网关IP
        proxy_http_version 1.1;
        proxy_read_timeout 360s;   
        proxy_redirect off;   
        proxy_set_header Upgrade $http_upgrade; 
        proxy_set_header Connection "upgrade";    #配置连接为升级连接
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

二、网关

1、routes配置

        - id: id-websocket
          uri: lb:ws://zzf-websocket
          predicates:
            - Path=/ws/**  

zzf-websocket 为websocket 微服务 服务名称

2、安全配置(白名单)
security:

   ignore:
     whites:
       - /ws/**

  

三、web

.env.production
# Websocket
VUE_APP_WS_URL = 'wss://域名.com/websocket/ws/'

vue: 

setupWebSocket() {try {this.websocket = new WebSocket(process.env.VUE_APP_WS_URL + '/orderWebsocket') // 创建WebSocket连接this.websocket.onopen = this.onWebSocketOpen // WebSocket连接打开时的处理函数this.websocket.onmessage = this.onWebSocketMessage // 收到WebSocket消息时的处理函数this.websocket.onclose = this.onWebSocketClose // WebSocket连接关闭时的处理函数} catch (e) {console.error(e)}},onWebSocketMessage(event) {const message = event.dataconsole.log('onWebSocketMessage {}',message)this.notifyOrder(message);this.$modal.notifySuccess(message);
},
onWebSocketClose() {console.log(this.reconnectCount+ ' WebSocket connection is closed')this.stopHeartbeat() // WebSocket连接关闭时,停止心跳检测if(this.reconnectCount>0){setTimeout(this.setupWebSocket, this.reconnectInterval) // 在一定时间后重连WebSocketthis.reconnectCount --;}},
startHeartbeat() {this.heartbeatInterval = setInterval(() => {if (this.websocket && this.websocket.readyState === WebSocket.OPEN) {this.websocket.send('ping') // 发送心跳消息}}, 10000) // 每10秒发送一次心跳
},
stopHeartbeat() {if (this.heartbeatInterval) {clearInterval(this.heartbeatInterval) // 停止心跳检测定时器}
},onWebSocketOpen() {console.log('链接成功')
},closeWebSocket() {if (this.websocket) {this.websocket.close() // 关闭WebSocket连接}
},

http://www.lryc.cn/news/369091.html

相关文章:

  • 基于JavaScript 实现近邻算法以及优化方案
  • 移动端适配和响应式页面中的常用单位
  • 麒麟v10系统arm64架构openssh9.7p1的rpm包
  • 刚刚❗️德勤2025校招暑期实习测评笔试SHL测评题库已发(答案)
  • python对视频进行帧处理以及裁减部分区域
  • Python栈的编程题目
  • ROS云课三分钟外传之CoppeliaSim_Edu_V4_1_0_Ubuntu16_04
  • day28回溯算法part04| 93.复原IP地址 78.子集 90.子集II
  • SpringBoot项目启动时“jar中没有主清单属性”异常
  • vAttention:用于在没有Paged Attention的情况下Serving LLM
  • Python实现Stack
  • Helm在线部署Longhorn(1.6.0版本)分布式存储
  • 算法题目学习汇总
  • DockerCompose中部署Jenkins(Docker Desktop在windows上数据卷映射)
  • 吊车报警的工作原理和使用场景_鼎跃安全
  • Spring5
  • vue面试题二
  • 软件设计师笔记-程序语言基础知识
  • 在Windows上安装VMWare Pro 16.2(虚拟机)并从零安装CentOS 7.6镜像过程记录
  • NGINX之location和rewrite
  • Python数据框的合并(一) -- merge函数
  • 【Qt秘籍】[010]-Qt常用控件
  • TypeScript基础教程学习
  • JavaSE面试
  • 安全漏洞扫描工具
  • 前端开发部署:Visual Studio Code + vue
  • 基于Sentry+OpenTelemetry实现微服务前后端全链路监控
  • jquery.datetimepicker无法添加清除按钮的问题
  • Qt中解决编译中文乱码和编译失败的问题
  • Android状态栏适配问题