Created
June 12, 2020 18:06
-
-
Save hindol/854ed24594e2909abfae7d45cd8cf391 to your computer and use it in GitHub Desktop.
Access WebSocket session in Pedestal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn ws-listener | |
[_request _response ws-map] | |
(proxy [WebSocketAdapter] [] | |
(onWebSocketConnect [^Session ws-session] | |
(proxy-super onWebSocketConnect ws-session) | |
(when-let [f (:on-connect ws-map)] | |
(f ws-session))) | |
(onWebSocketClose [status-code reason] | |
(when-let [f (:on-close ws-map)] | |
(f (.getSession this) status-code reason))) | |
(onWebSocketError [^Throwable e] | |
(when-let [f (:on-error ws-map)] | |
(f (.getSession this) e))) | |
(onWebSocketText [^String message] | |
(when-let [f (:on-text ws-map)] | |
(f (.getSession this) message))) | |
(onWebSocketBinary [^bytes payload offset length] | |
(when-let [f (:on-binary ws-map)] | |
(f (.getSession this) payload offset length))))) | |
;; in your service map: | |
::http/container-options {:context-configurator #(ws/add-ws-endpoints % ws-paths {:listener-fn ws-listener})} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want reloading to work when using the REPL, use
var
s for your handlers:In the example above, you can change
socket-on-text
, load it into your REPL, and the new function definition will handle new messages. For the ones baked into the map, they won't, and I couldn't see a better way to make that work (e.g., putting thevar
higher up in the chain, like we do for normal routes).