为什么 WebSocket 在每隔 7 分钟后会断开连接?(Angular 和 Go)

huangapple go评论98阅读模式
英文:

Why does websocket disconnect after every 7 minutes? (Angular and Go )

问题

我正在处理一个应用程序,在前端使用了Angular,在后端使用了Go,并使用了WebSocket(RxJs,Gorilla)。WebSocket连接每7分钟就会断开。我没有在任何地方设置7分钟的超时时间。我已经对ngnix进行了配置更改,将代理读取超时时间设置为7天,保持连接超时时间设置为7天。我们还使用了AWS ALB(负载均衡器)。不知道为什么会发生这种情况?请帮助我解决。我是否需要进行任何配置更改?因为我监控了应用程序,如果WebSocket连接是活动的,它就能正常工作,只是在几分钟后开始出现奇怪的反应。所以我有一种感觉,这不是代码的问题,而是一些配置问题。

英文:

I am working on the application where I have used angular in frontend and go in backend, using websocket ( RxJs, Gorilla ). The websocket connections disconnect every 7 minutes. I didn't set anywhere 7 minutes as timeout. I did ngnix config changes as proxy read timeout as 7d, keep alive timeout as 7d. Also we use Aws ALB( load balancer). No idea why it is happening? Please help me out. Do I need to make any config changes? Because I have monitored the application that if websocket connections are alive it is working fine, only after few minutes it started reacting weird. So I have feeling it is not the issue in the code, but some configuration

答案1

得分: 1

在RFC中,WebSocket不是一种保持连接和协议的方式。这意味着,你必须设计一个架构来验证并保持连接的活跃性。
参见RFC 6455和PING、PONG的定义。

一旦你的心跳信号保持对服务器端点的响应(据我所知,这个ping/pong也可以由客户端引导),底层协议层将识别此连接是否活跃,否则必须关闭以符合规范。

一些框架可能已经封装了内置的保持连接机制,所以首先检查它们的参考文档。
> 另外,Gorilla没有处理它,所以你必须手动实现。
> 但这是一个非常简单的工作,有很多示例代码,比如Gorilla WebSocket聊天示例:客户端和服务器以及SetPongHandler

顺便说一下,任何WebSocket层上的通信错误都应该导致WebSocket连接关闭。在这种情况下,应设计一个稳定的通信框架,以便能够管理重新连接事件。

英文:

In RFC, websocket is not a keep-alive connection and protocol. That means, you must design an architecture to verify and make it alive.
See also RFC 6455 and PING, PONG definitions.

Once your heartbeat signal keeps the response to the endpoint on server (as I knew, this ping/pong can be guided from client too), the underlying protocol layer will identify this connection is alive, or it must be closed for complying with the spec.

Some frameworks might have wrapped a builtin keep-alive mechanism for serve it, so check their references at first.
> And, Gorilla haven't handle it so you have to implement it manually.
> But this is a super-easy work and there're many sample codes, such as gorilla websocket chat sample: client and server, and SetPongHandler.

BTW, any communication errors upon websocket layer should cause the ws connect closed. In this case, a stable communication framework should be designed to have ability to manage the reconnecting event.

huangapple
  • 本文由 发表于 2022年8月20日 18:34:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/73425951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定