Skip to content

Commit

Permalink
Add -open? method to Socket protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
weavejester committed Sep 11, 2023
1 parent 94d4795 commit 551a9c1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion SPEC-alpha.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,15 @@ A socket must satisfy the `ring.websocket/Socket` protocol:

```clojure
(defprotocol Socket
(-open? [socket])
(-send [socket message])
(-ping [socket data])
(-pong [socket data])
(-close [socket status reason]))
```

The types of the arguments are the same as those described for the
`Listener` protocol.
`Listener` protocol. The `-open?` method must return true or false.

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

Expand Down
7 changes: 6 additions & 1 deletion ring-core/src/ring/websocket.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"Called when a pong is received in response to an earlier ping. The client
may provide additional binary data, represented by the data ByteBuffer.")
(on-error [listener socket throwable]
"Called when an Throwable error is thrown.")
"Called when a Throwable error is thrown.")
(on-close [listener socket code reason]
"Called when the websocket is closed, along with an integer code and a
plaintext string reason for being closed."))
Expand All @@ -35,6 +35,8 @@

(defprotocol Socket
"A protocol for sending data via websocket."
(-open? [socket]
"Returns true if the socket is open; false otherwise.")
(-send [socket message]
"Sends a String or ByteBuffer to the client via the websocket.")
(-ping [socket data]
Expand Down Expand Up @@ -83,6 +85,9 @@
:else (throw (ex-info "message is not a valid text or binary data type"
{:message message}))))

(defn open? [socket]
(boolean (-open? socket)))

(defn send
"Sends text or binary data via a websocket, either synchronously or
asynchronously with callback functions. A convenient wrapper for the -send and
Expand Down
2 changes: 2 additions & 0 deletions ring-jetty-adapter/src/ring/adapter/jetty.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
(let [remote (.getRemote session)]
(reify
ws/Socket
(-open? [_]
(.isOpen session))
(-send [_ message]
(if (string? message)
(.sendString remote message)
Expand Down
20 changes: 20 additions & 0 deletions ring-jetty-adapter/test/ring/adapter/test/jetty.clj
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,26 @@
(is (= [[:ping "foo"] [:pong "foo"]]
@log))))

(testing "open?"
(let [log (atom [])
handler (constantly
{::ws/listener
(reify ws/Listener
(on-open [_ sock]
(swap! log conj [:open? (ws/open? sock)])
(ws/close sock)
(swap! log conj [:open? (ws/open? sock)]))
(on-message [_ _ _])
(on-pong [_ _ data])
(on-error [_ _ _])
(on-close [_ _ code reason]
(swap! log conj [:close])))})]
(with-server handler {:port test-port}
(hato/websocket test-websocket-url {})
(Thread/sleep 100))
(is (= [[:open? true] [:open? false] [:close]]
@log))))

(testing "sending websocket messages asynchronously"
(let [log (atom [])
handler (constantly
Expand Down

0 comments on commit 551a9c1

Please sign in to comment.