本文档说明如何在不正式部署到线上服务器的前提下,把本机的 realtime-gateway 暴露给外部设备或远程联调方。
适用场景:
不适用场景:
当前阶段建议:
wss 地址这样做的好处:
推荐先使用:
这个配置相比开发默认配置更适合 Tunnel 联调:
18080建议先把其中的 token 改成你自己的值。
启动方式:
cd D:\dev\cmr-mini\realtime-gateway
go build -o .\bin\gateway.exe .\cmd\gateway
.\bin\gateway.exe -config .\config\tunnel-dev.json
本机地址:
http://127.0.0.1:18080ws://127.0.0.1:18080/wsQuick Tunnel 最适合当前阶段。
Cloudflare 官方文档说明:
http://localhost:8080 之类的地址暴露出去cloudflared tunnel --url http://localhost:8080来源:
你的网关如果跑在 18080:
cloudflared tunnel --url http://localhost:18080
启动后,cloudflared 会输出一个随机 trycloudflare.com 域名。
例如:
https://random-name.trycloudflare.com
对应的 WebSocket 地址就是:
wss://random-name.trycloudflare.com/ws
注意:
wss://http://localhost:18080Cloudflare 官方说明里,Quick Tunnel 当前有这些限制:
因此它适合:
不适合拿来当正式生产入口。
另外,官方文档提到:
.cloudflared 目录里已有 config.yaml,Quick Tunnel 可能不能直接使用来源:
如果你后面不想每次拿随机域名,可以改用 Named Tunnel。
这时推荐的本地 cloudflared 配置示例在:
示例内容:
tunnel: YOUR_TUNNEL_ID
credentials-file: C:\Users\YOUR_USER\.cloudflared\YOUR_TUNNEL_ID.json
ingress:
- hostname: gateway-dev.example.com
service: http://localhost:18080
- service: http_status:404
关键点:
gateway-dev.example.com 映射到本机 http://localhost:18080http_status:404 是 catch-allCloudflare 官方文档对 published application 和 ingress 的说明见:
启动后,对外 WebSocket 地址就是:
wss://gateway-dev.example.com/ws
如果你要在另一台机器上用仓库里的 mock-consumer 做联调,推荐复制:
consumer-tunnel.example.json consumer-gps-heart.example.json
填好实际的公网地址和 token,例如:
{
"url": "wss://gateway-dev.example.com/ws",
"token": "your-consumer-token",
"deviceId": "child-001",
"topics": [
"telemetry.location",
"telemetry.heart_rate"
],
"snapshot": true
}
然后运行:
cd D:\dev\cmr-mini\realtime-gateway
go run .\cmd\mock-consumer -config .\config\consumer-tunnel.example.json
命令行参数会覆盖配置文件中的同名项,所以临时换 deviceId 时可以直接追加:
go run .\cmd\mock-consumer -config .\config\consumer-tunnel.example.json -device-id child-002
如果只想临时直接从命令行看 GPS 和心率:
go run .\cmd\mock-consumer -url wss://gateway-dev.example.com/ws -token your-consumer-token -device-id child-001 -topics telemetry.location,telemetry.heart_rate
旧模拟器仍然本地运行:
cd D:\dev\cmr-mini
npm run mock-gps-sim
然后在旧模拟器页面的“新网关桥接”区域里填:
ws://127.0.0.1:18080/wstunnel-dev.json 里配置一致mock-gps-sim-a这里要注意:
不要把旧模拟器桥接目标直接写成公网 wss 地址。
旧模拟器和网关都在本机,直接走本地回环最稳。
旧模拟器页面
-> 本机 mock-gps-sim
-> 本机 realtime-gateway (ws://127.0.0.1:18080/ws)
-> Cloudflare Tunnel
-> 外部 consumer / 真机 / 调试端 (wss://<public-host>/ws)
这样职责最清晰:
即使只是联调,也建议至少做到:
allowAnonymousConsumers = false如果只是你自己本机调试:
realtime-gateway 暴露出去当前阶段最推荐的方案是:
ws://127.0.0.1:18080/wscloudflared tunnel --url http://localhost:18080 先跑 Quick Tunnelwss://<quick-tunnel-domain>/ws这条路径最轻、最稳,也最符合你现在“先不正式上线”的目标。