Skip to content

Commit 0103527

Browse files
committed
Improve WebSocket docs and spec
1 parent 1632c47 commit 0103527

File tree

2 files changed

+64
-15
lines changed

2 files changed

+64
-15
lines changed

SPEC-alpha.md

+61-14
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ It may also be used from an asynchronous handler.
242242
```clojure
243243
(fn [request respond raise]
244244
(respond #:ring.websocket{:listener websocket-listener}))
245+
```
245246

246247
A websocket response contains the following keys. Any key not marked as
247248
**required** may be omitted.
@@ -275,14 +276,35 @@ protocol:
275276
(on-close [listener socket code reason]))
276277
```
277278

278-
The arguments are described as follows:
279+
#### on-open
280+
281+
Called once when the websocket is first opened. Supplies a `socket`
282+
argument that satisfies `ring.websocket/Socket`, described in section
283+
3.3.
284+
285+
#### on-message
286+
287+
Called when a text or binary message frame is received from the client.
288+
The `message` argument may be a `String` or a `java.nio.ByteBuffer`
289+
depending on whether the message is text or binary.
290+
291+
#### on-pong
292+
293+
Called when a "pong" frame is received from the client. The `data`
294+
argument is a `java.nio.ByteBuffer` that contains optional client
295+
session data.
279296

280-
* `socket` - described in section 3.3.
281-
* `message` - a `String` or `java.nio.ByteBuffer` containing a message
282-
* `data` - a `java.nio.ByteBuffer` containing pong data
283-
* `throwable` - an error inheriting from `java.lang.Throwable`
284-
* `code` - an integer from 1000 to 4999
285-
* `reason` - a string describing the reason for closing the socket
297+
#### on-error
298+
299+
Called when an error occurs. This may cause the websocket to be closed.
300+
The `throwable` argument is a `java.lang.Throwable` that was generated
301+
by the error.
302+
303+
#### on-close
304+
305+
Called once when the websocket is closed. Guaranteed to be called, even
306+
if an error occurs, so may be used for finalizing/cleanup logic. Takes
307+
an integer `code` and a string `reason` as arguments.
286308

287309
### 3.3. Websocket Sockets
288310

@@ -294,19 +316,44 @@ A socket must satisfy the `ring.websocket/Socket` protocol:
294316
(-send [socket message])
295317
(-ping [socket data])
296318
(-pong [socket data])
297-
(-close [socket status reason]))
319+
(-close [socket code reason]))
298320
```
299321

300-
The types of the arguments are the same as those described for the
301-
`Listener` protocol. The `-open?` method must return true or false.
302-
303322
It *may* optionally satisfy the `ring.websocket/AsyncSocket` protocol:
304323

305324
```clojure
306325
(defprotocol AsyncSocket
307326
(-send-async [socket message succeed fail]))
308327
```
309328

310-
Where `succeed` is a callback function that expects zero arguments, and
311-
`fail` is a callback function expecting a single `java.lang.Throwable`
312-
argument.
329+
#### -open?
330+
331+
Returns a truthy or falsey value denoting whether the socket is
332+
currently connected to the client.
333+
334+
#### -send
335+
336+
Sends a websocket message frame that may be a `String` (for text), or
337+
a `java.nio.ByteBuffer` (for binary).
338+
339+
#### -send-async
340+
341+
As above, but does not block and requires two callback functions:
342+
343+
- `succeed` is called with zero arguments when the send succeeds
344+
- `fail` is called with a single `java.lang.Throwable` argument when the
345+
send fails
346+
347+
#### -ping
348+
349+
Sends a websocket ping frame with a `java.nio.ByteBuffer` of session
350+
data (which may be empty).
351+
352+
#### -pong
353+
354+
Sends an unsolicited pong frame with a `java.nio.ByteBuffer` of session
355+
data (which may be empty).
356+
357+
#### -close
358+
359+
Closes the websocket with the supplied integer code and reason string.

ring-core/src/ring/websocket.clj

+3-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@
8686
:else (throw (ex-info "message is not a valid text or binary data type"
8787
{:message message}))))
8888

89-
(defn open? [socket]
89+
(defn open?
90+
"Returns true if the Socket is open, false otherwise."
91+
[socket]
9092
(boolean (-open? socket)))
9193

9294
(defn send

0 commit comments

Comments
 (0)