Skip to content

Commit 94d4795

Browse files
committed
Add docstrings to ring.websocket namespace
1 parent 4ab0f5e commit 94d4795

File tree

1 file changed

+58
-14
lines changed

1 file changed

+58
-14
lines changed

ring-core/src/ring/websocket.clj

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
(ns ring.websocket
2+
"Protocols and utility functions for websocket support."
23
(:refer-clojure :exclude [send])
34
(:import [java.nio ByteBuffer]))
45

56
(defprotocol Listener
6-
(on-open [listener socket])
7-
(on-message [listener socket message])
8-
(on-pong [listener socket data])
9-
(on-error [listener socket throwable])
10-
(on-close [listener socket code reason]))
7+
"A protocol for handling websocket events. The second argument is
8+
always an object that satisfies the Socket protocol."
9+
(on-open [listener socket]
10+
"Called when the websocket is opened.")
11+
(on-message [listener socket message]
12+
"Called when a message is received. The message may be a String or a
13+
ByteBuffer.")
14+
(on-pong [listener socket data]
15+
"Called when a pong is received in response to an earlier ping. The client
16+
may provide additional binary data, represented by the data ByteBuffer.")
17+
(on-error [listener socket throwable]
18+
"Called when an Throwable error is thrown.")
19+
(on-close [listener socket code reason]
20+
"Called when the websocket is closed, along with an integer code and a
21+
plaintext string reason for being closed."))
1122

1223
(extend-protocol Listener
1324
clojure.lang.IPersistentMap
@@ -23,19 +34,37 @@
2334
(when-let [kv (find m :on-close)] ((val kv) socket code reason))))
2435

2536
(defprotocol Socket
26-
(-send [socket message])
27-
(-ping [socket data])
28-
(-pong [socket data])
29-
(-close [socket status reason]))
37+
"A protocol for sending data via websocket."
38+
(-send [socket message]
39+
"Sends a String or ByteBuffer to the client via the websocket.")
40+
(-ping [socket data]
41+
"Sends a ping message to the client with a ByteBuffer of extra data.")
42+
(-pong [socket data]
43+
"Sends an unsolicited pong message to the client, with a ByteBuffer of extra
44+
data.")
45+
(-close [socket code reason]
46+
"Closes the socket with an integer status code, and a String reason."))
3047

3148
(defprotocol AsyncSocket
32-
(-send-async [socket message succeed fail]))
49+
"A protocol for sending data asynchronously via websocket. Intended for use
50+
with the Socket protocol."
51+
(-send-async [socket message succeed fail]
52+
"Sends a String or ByteBuffer to the client via the websocket. If it
53+
succeeds, the 'succeed' callback function is called with zero arguments. If
54+
it fails, the 'fail' callback function is called with the exception that was
55+
thrown."))
3356

3457
(defprotocol TextData
35-
(->string [data]))
58+
"A protocol for converting text data into a String."
59+
(->string [data]
60+
"Convert some data into a String, ready to be sent as a websocket text
61+
message."))
3662

3763
(defprotocol BinaryData
38-
(->byte-buffer [data]))
64+
"A protocol for converting binary data into a java.nio.ByteBuffer object."
65+
(->byte-buffer [data]
66+
"Convert some binary data into a java.nio.ByteBuffer, ready to be sent as
67+
a websocket binary message."))
3968

4069
(extend-protocol TextData
4170
String
@@ -55,33 +84,48 @@
5584
{:message message}))))
5685

5786
(defn send
87+
"Sends text or binary data via a websocket, either synchronously or
88+
asynchronously with callback functions. A convenient wrapper for the -send and
89+
-send-async protocol methods."
5890
([socket message]
5991
(-send socket (encode-message message)))
6092
([socket message succeed fail]
6193
(-send-async socket (encode-message message) succeed fail)))
6294

6395
(defn ping
96+
"Sends a ping message via a websocket, with an optional byte array or
97+
ByteBuffer that may contain custom session data. A convenient wrapper for the
98+
-ping protocol method."
6499
([socket]
65100
(-ping socket (ByteBuffer/allocate 0)))
66101
([socket data]
67102
(-ping socket (->byte-buffer data))))
68103

69104
(defn pong
105+
"Sends an unsolicited pong message via a websocket, with an optional byte
106+
array or ByteBuffer that may contain custom session data. A convenient wrapper
107+
for the -pong protocol method."
70108
([socket]
71109
(-pong socket (ByteBuffer/allocate 0)))
72110
([socket data]
73111
(-pong socket (->byte-buffer data))))
74112

75113
(defn close
114+
"Closes the websocket, with an optional custom integer status code and reason
115+
string."
76116
([socket]
77117
(-close socket 1000 "Normal Closure"))
78118
([socket code reason]
79119
(-close socket code reason)))
80120

81-
(defn websocket-request? [request]
121+
(defn websocket-request?
122+
"Returns true if the request map expects a websocket response."
123+
[request]
82124
(let [headers (:headers request)]
83125
(and (.equalsIgnoreCase "upgrade" (get headers "connection"))
84126
(.equalsIgnoreCase "websocket" (get headers "upgrade")))))
85127

86-
(defn websocket-response? [response]
128+
(defn websocket-response?
129+
"Returns true if the response contains a websocket listener."
130+
[response]
87131
(contains? response ::listener))

0 commit comments

Comments
 (0)