Skip to content

Commit 46738db

Browse files
committed
Add -open? method to Socket protocol
While technically one could use the on-close event to keep track of whether a socket is open, it is more convenient to allow the socket to be queried directly.
1 parent 94d4795 commit 46738db

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

SPEC-alpha.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff 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

283284
The 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

286287
It *may* optionally satisfy the `ring.websocket/AsyncSocket` protocol:
287288

ring-core/src/ring/websocket.clj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
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."))
@@ -35,6 +35,8 @@
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]
@@ -83,6 +85,9 @@
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

ring-jetty-adapter/src/ring/adapter/jetty.clj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
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)

ring-jetty-adapter/test/ring/adapter/test/jetty.clj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,26 @@
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

0 commit comments

Comments
 (0)