Skip to content

Commit

Permalink
Remove automatic channel close in go-websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
weavejester committed May 4, 2024
1 parent f7be8a4 commit 4d5ee4a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ The macro sets three binding variables:
* `out` - the output channel
* `err` - the error channel

Then executes its body in a core.async `go` block. When the block
completes, the websocket is closed by the server. If the client closes
the websocket, the associated channels are also closed.
Then executes its body in a core.async `go` block. If the WebSocket is
closed by either the client or the server, the associated channels will
also be closed.

A Ring websocket response will be returned by the macro, so you can
directly return it from the handler.
Expand Down
6 changes: 3 additions & 3 deletions src/ring/websocket/async.clj
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"Macro for returning a websocket response handled by an inner go block.
Expects three binding symbols - in, out and err - and assigns them to
channels (see: websocket-listener). The body of the macro is executed in a
core.async go block. At the end of the body, the socket is closed.
core.async go block.
The err symbol may optionally be omitted. In that case, any websocket
error will result in the socket being closed with a 1011 unexpected
Expand All @@ -68,12 +68,12 @@
`(let [~in (a/chan)
~out (a/chan)
~err (a/chan)]
(a/go (try ~@body (finally (a/close! ~out))))
(a/go ~@body)
{::ws/listener (websocket-listener ~in ~out ~err)})
`(let [~in (a/chan)
~out (a/chan)
err# (a/chan)]
(a/go (when-let [ex# (a/<! err#)]
(a/>! ~out (closed 1011 "Unexpected Error"))))
(a/go (try ~@body (finally (a/close! ~out))))
(a/go ~@body)
{::ws/listener (websocket-listener ~in ~out err#)})))
9 changes: 6 additions & 3 deletions test/ring/websocket/async_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
(a/>! server [:receive (a/<! in)])
(a/>! out "Second")
(a/>! server [:receive (a/<! in)])
(a/>! out "Fourth"))
(a/>! out "Fourth")
(a/close! out))
listener (::ws/listener response)]
(is (map? response))
(is (satisfies? wsp/Listener listener))
Expand All @@ -107,7 +108,8 @@
response (wsa/go-websocket [_ out err]
(a/>! server (a/<! err))
(a/>! out "expected failure")
(a/>! server (a/<! err)))
(a/>! server (a/<! err))
(a/close! out))
listener (::ws/listener response)]
(is (satisfies? wsp/Listener listener))
(wsp/on-open listener socket)
Expand Down Expand Up @@ -145,7 +147,8 @@
response (wsa/go-websocket [in out]
(a/>! server [:receive (a/<! in)])
(a/>! out "Second")
(a/>! server [:receive (a/<! in)]))
(a/>! server [:receive (a/<! in)])
(a/close! out))
listener (::ws/listener response)]
(wsp/on-open listener socket)
(wsp/on-message listener socket "First")
Expand Down

0 comments on commit 4d5ee4a

Please sign in to comment.