File tree Expand file tree Collapse file tree 4 files changed +30
-2
lines changed Expand file tree Collapse file tree 4 files changed +30
-2
lines changed Original file line number Diff line number Diff line change @@ -274,14 +274,15 @@ A socket must satisfy the `ring.websocket/Socket` protocol:
274274
275275``` clojure
276276(defprotocol Socket
277+ (-open? [socket])
277278 (-send [socket message])
278279 (-ping [socket data])
279280 (-pong [socket data])
280281 (-close [socket status reason]))
281282```
282283
283284The types of the arguments are the same as those described for the
284- ` Listener ` protocol.
285+ ` Listener ` protocol. The ` -open? ` method must return true or false.
285286
286287It * may* optionally satisfy the ` ring.websocket/AsyncSocket ` protocol:
287288
Original file line number Diff line number Diff line change 1515 " Called when a pong is received in response to an earlier ping. The client
1616 may provide additional binary data, represented by the data ByteBuffer." )
1717 (on-error [listener socket throwable ]
18- " Called when an Throwable error is thrown." )
18+ " Called when a Throwable error is thrown." )
1919 (on-close [listener socket code reason]
2020 " Called when the websocket is closed, along with an integer code and a
2121 plaintext string reason for being closed." ))
3535
3636(defprotocol Socket
3737 " A protocol for sending data via websocket."
38+ (-open? [socket]
39+ " Returns true if the socket is open; false otherwise." )
3840 (-send [socket message]
3941 " Sends a String or ByteBuffer to the client via the websocket." )
4042 (-ping [socket data]
8385 :else (throw (ex-info " message is not a valid text or binary data type"
8486 {:message message}))))
8587
88+ (defn open? [socket]
89+ (boolean (-open? socket)))
90+
8691(defn send
8792 " Sends text or binary data via a websocket, either synchronously or
8893 asynchronously with callback functions. A convenient wrapper for the -send and
Original file line number Diff line number Diff line change 3737 (let [remote (.getRemote session)]
3838 (reify
3939 ws/Socket
40+ (-open? [_]
41+ (.isOpen session))
4042 (-send [_ message]
4143 (if (string? message)
4244 (.sendString remote message)
Original file line number Diff line number Diff line change 718718 (is (= [[:ping " foo" ] [:pong " foo" ]]
719719 @log))))
720720
721+ (testing " open?"
722+ (let [log (atom [])
723+ handler (constantly
724+ {::ws/listener
725+ (reify ws/Listener
726+ (on-open [_ sock]
727+ (swap! log conj [:open? (ws/open? sock)])
728+ (ws/close sock)
729+ (swap! log conj [:open? (ws/open? sock)]))
730+ (on-message [_ _ _])
731+ (on-pong [_ _ data])
732+ (on-error [_ _ _])
733+ (on-close [_ _ code reason]
734+ (swap! log conj [:close ])))})]
735+ (with-server handler {:port test-port}
736+ (hato/websocket test-websocket-url {})
737+ (Thread/sleep 100 ))
738+ (is (= [[:open? true ] [:open? false ] [:close ]]
739+ @log))))
740+
721741 (testing " sending websocket messages asynchronously"
722742 (let [log (atom [])
723743 handler (constantly
You can’t perform that action at this time.
0 commit comments