stalefruits / gniazdo

A WebSocket client for Clojure
Apache License 2.0
208 stars 38 forks source link

Websocket Ping and Pong messages #32

Open theronic opened 5 years ago

theronic commented 5 years ago

How do I tell Gniazdo tot tell Jetty to keep the websocket connection alive with ping & pong messages every N seconds?

My websocket client connections to a server expecting pings are being forceably closed (code: 1006) because I'm not keeping the connection alive with ping/pong messages.

theronic commented 5 years ago

The closest thing I can find is this clj-pcp-client source file, which implements its own Websocket client interface to trigger a heartbeart and call .sendPing on the underlying socket implementation. It would be convenient if Gniazdo would give me access to the underlying websocket-client so I didn't have to mock out the whole interface (maybe it does already).

theronic commented 5 years ago

Managed to implement heartbeat pinging like this:

(ws/connect remote-host
    :on-connect (fn [session]
        (go-schedule! ws-heartbeat-interval-ms kill-chan
            #(when (.isOpen session)
                 (log/debug "Pinging " session (.. session (getRemote) (sendPing (ByteBuffer/allocate 1)))))))
    ....
devth commented 4 years ago

@theronic awesome. Did you figure out how to listen for the corresponding pong event?