How to send PING frame using websockets?

1,395 views
Skip to first unread message

ant...@indb.uk

unread,
Aug 21, 2015, 7:43:28 PM8/21/15
to golang-nuts
How can I send a PING frame through websocket? The package seems to respond to PING frames [0] but how can I send the PING? Is there any other standard method to detect if the connection is dead?

[0] https://github.com/golang/net/blob/master/websocket/hybi.go#L302

ant...@indb.uk

unread,
Aug 22, 2015, 6:28:34 AM8/22/15
to golang-nuts
Gorilla API looks quite bad so I will skip it for now.

On Saturday, August 22, 2015 at 3:27:13 AM UTC+1, gary b wrote:
The Gorilla Websocket package supports pings.  

Matt Harden

unread,
Aug 22, 2015, 10:59:04 AM8/22/15
to ant...@indb.uk, golang-nuts
It looks to me like you could send a ping by setting conn.PayloadType to PingFrame and then using Write to send the ping; unfortunately you can't read the pong with Read because it will just ignore that frame type. It might be possible to do it by creating a Codec for it; I'm not sure.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Caleb Spare

unread,
Aug 22, 2015, 3:28:43 PM8/22/15
to ant...@indb.uk, golang-nuts
Gorilla API looks quite bad so I will skip it for now.

IMO the Gorilla websocket package should be preferred to the one in /x/net specifically because it has a superior API (it better represents the message-oriented nature of the protocol). There was a thread a while ago about fixing the /x/net API to make it more in line with gorilla/websocket, but that didn't seem to go anywhere.

The Gorilla implementation also seemed more fully featured the last time I looked into it (which admittedly was a while ago).

I've built sites using /x/net/websocket but all my future websocket projects will use gorilla/websocket.

-Caleb
Reply all
Reply to author
Forward
Message has been deleted
0 new messages