Skip to content

Commit

Permalink
Change websocket text frames to use CharSequence
Browse files Browse the repository at this point in the history
For sending text data via an I/O channel, a java.lang.CharSequence
may be more flexible and efficient than a String (which implements
the CharSequence interface).
  • Loading branch information
weavejester committed Oct 8, 2023
1 parent 2e75bbb commit 4a69cda
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions SPEC-alpha.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ argument that satisfies `ring.websocket/Socket`, described in section
#### on-message

Called when a text or binary message frame is received from the client.
The `message` argument may be a `String` or a `java.nio.ByteBuffer`
depending on whether the message is text or binary.
The `message` argument must be a `java.lang.CharSequence` or a
`java.nio.ByteBuffer` depending on whether the message is text or binary.

#### on-ping

Expand Down Expand Up @@ -353,8 +353,8 @@ currently connected to the client.

#### -send

Sends a websocket message frame that may be a `String` (for text), or
a `java.nio.ByteBuffer` (for binary).
Sends a websocket message frame that may be a `java.lang.CharSequence`
(for text), or a `java.nio.ByteBuffer` (for binary).

#### -send-async

Expand Down
12 changes: 6 additions & 6 deletions ring-core/src/ring/websocket.clj
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@

(defprotocol TextData
"A protocol for converting text data into a String."
(->string [data]
"Convert some data into a String, ready to be sent as a websocket text
message."))
(->char-sequence [data]
"Convert some data into a CharSequence, ready to be sent as a websocket
text message."))

(defprotocol BinaryData
"A protocol for converting binary data into a java.nio.ByteBuffer object."
Expand All @@ -82,8 +82,8 @@
a websocket binary message."))

(extend-protocol TextData
String
(->string [s] s))
CharSequence
(->char-sequence [cs] cs))

(extend-protocol BinaryData
(Class/forName "[B")
Expand All @@ -93,7 +93,7 @@

(defn- encode-message [message]
(cond
(satisfies? TextData message) (->string message)
(satisfies? TextData message) (->char-sequence message)
(satisfies? BinaryData message) (->byte-buffer message)
:else (throw (ex-info "message is not a valid text or binary data type"
{:message message}))))
Expand Down
8 changes: 4 additions & 4 deletions ring-jetty-adapter/src/ring/adapter/jetty.clj
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
(-open? [_]
(.isOpen session))
(-send [_ message]
(if (string? message)
(.sendString remote message)
(if (instance? CharSequence message)
(.sendString remote (.toString ^CharSequence message))
(.sendBytes remote message)))
(-ping [_ data]
(.sendPing remote data))
Expand All @@ -57,8 +57,8 @@
(let [callback (reify WriteCallback
(writeSuccess [_] (succeed))
(writeFailed [_ ex] (fail ex)))]
(if (string? message)
(.sendString remote message callback)
(if (instance? CharSequence message)
(.sendString remote (.toString ^CharSequence message) callback)
(.sendBytes remote message callback)))))))

(defn- websocket-listener [listener]
Expand Down

0 comments on commit 4a69cda

Please sign in to comment.