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

在Google Cloud Run部署WebSocket服务器连接问题排查

调试Google Cloud Run上WebSocket服务器连接失败问题

我在Google Cloud Run上部署了一个基于WebSocket的WebRTC信令服务器,服务指标显示已启动,但使用uwsc工具尝试以下连接时均失败,报错Error connecting to websocket:

  • uwsc ...run.app:9080/
  • uwsc ws://...run.app
  • uwsc wss://...run.app

GCP日志显示:

  • 使用ws://连接时,服务器返回302状态码
  • 使用wss://连接时,服务器返回101状态码(表示握手成功),但随即以1006代码关闭连接

服务器代码:

const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: process.env.PORT });

wss.on('connection', (ws) => {
  console.log(`Got a new connection`);
    ws.on('message', (message) => {
    console.log(`Got message ${message}.`);
    });
    ws.on('close', (code, reason) => {
        console.log(`Connection with peer closed `
            + `with reason ${code}: ${reason}`);
    });
    ws.on('error', (error) => {
        console.error(error);
    });
});

package.json内容:

{
  "name": "my-server",
  "version": "1.0.0",
  "private": true,
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "engines": {
    "node": ">=12.0.0"
  },
  "dependencies": {
    "ws": ">=8.0.0"
  }
}

调试步骤

1. 修正端口与连接路径配置

Cloud Run仅暴露**8080(HTTP)和443(HTTPS)**端口,不支持自定义端口如9080,因此...run.app:9080的连接必然失败。正确的连接方式应为:

  • 使用wss://<服务域名>(默认443端口,无需手动指定)
  • 确保路径为根路径/,即完整连接命令:uwsc wss://<服务域名>/

2. 兼容Cloud Run的HTTP服务要求

Cloud Run的负载均衡器会先发送HTTP请求做健康检查和路由,纯WebSocket服务器无法处理这类请求,会导致连接中断。需修改代码,添加HTTP服务器兼容层:

const http = require('http');
const WebSocket = require('ws');

// 创建HTTP服务器处理健康检查和基础请求
const server = http.createServer((req, res) => {
  res.writeHead(200);
  res.end('WebSocket server running\n');
});

// 将WebSocket服务器绑定到HTTP服务器上,指定根路径
const wss = new WebSocket.Server({ server, path: '/' });

wss.on('connection', (ws) => {
  console.log(`Got a new connection`);
  ws.on('message', (message) => {
    console.log(`Got message ${message}.`);
  });
  ws.on('close', (code, reason) => {
    console.log(`Connection closed: code=${code}, reason=${reason.toString()}`);
  });
  ws.on('error', (error) => {
    console.error(error);
  });
});

server.listen(process.env.PORT, () => {
  console.log(`Server listening on port ${process.env.PORT}`);
});

3. 检查健康检查配置

Cloud Run默认会对服务执行健康检查,如果服务无法处理HTTP GET请求,健康检查会失败,导致服务无法正常接收流量。修改后的代码已支持根路径的GET请求,也可在Cloud Run控制台「容器」→「健康检查」中,调整检查路径和阈值,确保与服务实现匹配。

4. 排查1006关闭代码原因

1006表示连接异常关闭,常见原因及排查方式:

  • 打印更详细的关闭原因:修改close事件逻辑,将reason Buffer转为字符串输出,便于定位问题
  • 查看Cloud Run详细日志:检查是否有负载均衡器发送的超时或强制关闭信号
  • 检查网络策略:确认服务未被VPC防火墙或Cloud Run的访问控制拦截

5. 本地验证代码正确性

在本地设置PORT=8080后启动服务器,用uwsc ws://localhost:8080/测试连接。如果本地能正常通信,说明问题出在Cloud Run的配置上,而非代码逻辑。

6. 检查Cloud Run服务配置

  • 开启「允许未验证的调用者」:若未配置身份验证,需确保该选项开启,否则连接会被拦截
  • 容器端口设置为8080:Cloud Run默认使用8080端口,必须与代码监听端口一致
  • 调整CPU分配模式:若使用「按需分配CPU」,空闲时CPU会被回收,可能导致连接中断,建议暂时改为「始终分配CPU」测试。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 06:12:38