Skip to content

Commit 0d7f29a

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

File tree

2 files changed

+63
-15
lines changed

2 files changed

+63
-15
lines changed

SPEC-alpha.md

+60-14
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,35 @@ protocol:
275275
(on-close [listener socket code reason]))
276276
```
277277

278-
The arguments are described as follows:
278+
#### on-open
279279

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
280+
Called once when the websocket is first opened. Supplies a `socket`
281+
argument that satisfies `ring.websocket/Socket`, described in section
282+
3.3.
283+
284+
#### on-message
285+
286+
Called when a text or binary message frame is received from the client.
287+
The `message` argument may be a `String` or a `java.nio.ByteBuffer`
288+
depending on whether the message is text or binary.
289+
290+
#### on-pong
291+
292+
Called when a "pong" frame is received from the client. The `data`
293+
argument is a `java.nio.ByteBuffer` that contains optional client
294+
session data.
295+
296+
#### on-error
297+
298+
Called when an error occurs. This may cause the websocket to be closed.
299+
The `throwable` argument is a `java.lang.Throwable` that was generated
300+
by the error.
301+
302+
#### on-close
303+
304+
Called once when the websocket is closed. Guaranteed to be called, even
305+
if an error occurs, so may be used for finalizing/cleanup logic. Takes
306+
an integer `code` and a string `reason` as arguments.
286307

287308
### 3.3. Websocket Sockets
288309

@@ -294,19 +315,44 @@ A socket must satisfy the `ring.websocket/Socket` protocol:
294315
(-send [socket message])
295316
(-ping [socket data])
296317
(-pong [socket data])
297-
(-close [socket status reason]))
318+
(-close [socket code reason]))
298319
```
299320

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-
303321
It *may* optionally satisfy the `ring.websocket/AsyncSocket` protocol:
304322

305323
```clojure
306324
(defprotocol AsyncSocket
307325
(-send-async [socket message succeed fail]))
308326
```
309327

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