如何在GO的websocket中保持连接活跃 [英] How to keep connection alive in GO's websocket
问题描述
我在服务器中使用 code.google.com/p/go.net/websocket
,因此客户端可以从服务器获取通知。然而,在客户端连接到服务器后,如果客户端和服务器之间没有任何数据传输,服务器将在 websocket处返回EOF错误.JSON.Receive()
,它看起来像一个超时机制。
我在Google搜索过,似乎websocket协议有一个乒乓心跳维持连接,我想问一下 code.google.com/p/go.net/websocket
是否支持此ping协议?
如果我想保持客户端之间的连接,应该怎么做和服务器还活着?
最近2013年,go.net websocket库不支持(自动)keep-alive消息。您有两种选择:通过定期让应用程序沿管道发送消息(任一方向)来实现应用程序级别保持活动状态应该可以工作),这被另一方忽略。
I use code.google.com/p/go.net/websocket
in server, so client can get notification from server.
however, It seems after client connected to server, if there is no any data tranfer between client and server, server will return EOF error at websocket.JSON.Receive()
, it looks like a timeout mechanism.
And I have search in Google, it seems websocket protocol has a ping-pong heartbeat to maintain the connection, I want to ask whether code.google.com/p/go.net/websocket
support this ping protocol or not?
What should I do if I want keep connection between client and server alive?
As recently as 2013, the go.net websocket library does not support (automatic) keep-alive messages. You have two options:
- Implement an "application level" keep-alive by periodically having your application send a message down the pipe (either direction should work), that is ignored by the other side.
- Move to a different websocket library that does support keep-alives (like this one) Edit: it looks like that library has been superseded by Gorilla websockets.
这篇关于如何在GO的websocket中保持连接活跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!