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:
274
274
275
275
``` clojure
276
276
(defprotocol Socket
277
+ (-open? [socket])
277
278
(-send [socket message])
278
279
(-ping [socket data])
279
280
(-pong [socket data])
280
281
(-close [socket status reason]))
281
282
```
282
283
283
284
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.
285
286
286
287
It * may* optionally satisfy the ` ring.websocket/AsyncSocket ` protocol:
287
288
Original file line number Diff line number Diff line change 15
15
" Called when a pong is received in response to an earlier ping. The client
16
16
may provide additional binary data, represented by the data ByteBuffer." )
17
17
(on-error [listener socket throwable ]
18
- " Called when an Throwable error is thrown." )
18
+ " Called when a Throwable error is thrown." )
19
19
(on-close [listener socket code reason]
20
20
" Called when the websocket is closed, along with an integer code and a
21
21
plaintext string reason for being closed." ))
35
35
36
36
(defprotocol Socket
37
37
" A protocol for sending data via websocket."
38
+ (-open? [socket]
39
+ " Returns true if the socket is open; false otherwise." )
38
40
(-send [socket message]
39
41
" Sends a String or ByteBuffer to the client via the websocket." )
40
42
(-ping [socket data]
83
85
:else (throw (ex-info " message is not a valid text or binary data type"
84
86
{:message message}))))
85
87
88
+ (defn open? [socket]
89
+ (boolean (-open? socket)))
90
+
86
91
(defn send
87
92
" Sends text or binary data via a websocket, either synchronously or
88
93
asynchronously with callback functions. A convenient wrapper for the -send and
Original file line number Diff line number Diff line change 37
37
(let [remote (.getRemote session)]
38
38
(reify
39
39
ws/Socket
40
+ (-open? [_]
41
+ (.isOpen session))
40
42
(-send [_ message]
41
43
(if (string? message)
42
44
(.sendString remote message)
Original file line number Diff line number Diff line change 718
718
(is (= [[:ping " foo" ] [:pong " foo" ]]
719
719
@log))))
720
720
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
+
721
741
(testing " sending websocket messages asynchronously"
722
742
(let [log (atom [])
723
743
handler (constantly
You can’t perform that action at this time.
0 commit comments