Skip to content

Instantly share code, notes, and snippets.

@dvingo
Forked from hindol/websocket.clj
Created June 12, 2020 18:32
Show Gist options
  • Save dvingo/fce8720a40b2b18fc027c49727f7db25 to your computer and use it in GitHub Desktop.
Save dvingo/fce8720a40b2b18fc027c49727f7db25 to your computer and use it in GitHub Desktop.
Access WebSocket session in Pedestal
(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