update how browserchannel server-side application event handling works

up until now, a single event handler "on-session" could be provided to
wrap-browserchannel which would be invoked each time a new client
session was created. it was then the application's responsibility to
register listeners for close/receive events inside this on-session event
handler.

this was kind of clunky honestly. basically every application would want
to use all of these events, so why not reduce the boilerplate needed?

this change makes it so a map of event handlers (:on-open, :on-close and
:on-receive) can be passed under wrap-browserchannel's options map (in
the key :events). this makes it simpler for an application to set up
browserchannel event handlers, and it works in basically the exact same
way as the clientside event handler registration works right now
This commit is contained in:
Gered 2016-05-08 18:44:54 -04:00
parent ecb66421b0
commit da2b5d94b2
2 changed files with 24 additions and 26 deletions

View file

@ -14,30 +14,26 @@
(defonce clients (atom #{}))
(defn on-browserchannel-session
[session-id request]
(println "session " session-id "connected")
(def event-handlers
{:on-open
(fn [session-id request]
(println "session " session-id "connected")
(swap! clients conj session-id)
(doseq [client-id @clients]
(browserchannel/send-map client-id {"msg" (str "client " session-id " connected")})))
(browserchannel/add-listener
session-id
:close
(fn [request reason]
(println "session " session-id " disconnected: " reason)
(swap! clients disj session-id)
(doseq [client-id @clients]
(browserchannel/send-map client-id {"msg" (str "client " session-id " disconnected " reason)}))))
:on-close
(fn [session-id request reason]
(println "session " session-id " disconnected: " reason)
(swap! clients disj session-id)
(doseq [client-id @clients]
(browserchannel/send-map client-id {"msg" (str "client " session-id " disconnected " reason)})))
(browserchannel/add-listener
session-id
:map
(fn [request map]
(println "session " session-id " sent " map)
(doseq [client-id @clients]
(browserchannel/send-map client-id map))))
(swap! clients conj session-id)
(doseq [client-id @clients]
(browserchannel/send-map client-id {"msg" (str "client " session-id " connected")})))
:on-receive
(fn [session-id request m]
(println "session " session-id " sent " m)
(doseq [client-id @clients]
(browserchannel/send-map client-id m)))})
(def app-routes
(routes
@ -50,7 +46,7 @@
(def handler
(-> app-routes
(browserchannel/wrap-browserchannel {:base "/channel" :on-session on-browserchannel-session})
(browserchannel/wrap-browserchannel {:base "/channel" :events event-handlers})
(wrap-defaults site-defaults)))
(defn run-jetty []

View file

@ -155,7 +155,7 @@
(send-off listeners-agent
(fn [listeners]
(doseq [callback (get-in listeners [session-id event-key])]
(apply callback request data))
(apply callback session-id request data))
listeners)))
;; end of listeners
@ -520,8 +520,10 @@
(set-error-handler! session-agent (agent-error-handler-fn (str "session-" (:id session))))
(set-error-mode! session-agent :continue)
(swap! sessions assoc id session-agent)
(when-let [notify (:on-session options)]
(notify id req))
(let [{:keys [on-open on-close on-receive]} (:events options)]
(if on-close (add-listener id :close on-close))
(if on-receive (add-listener id :map on-receive))
(if on-open (on-open id req)))
session-agent)))
(defn session-status [session]