Fork me on GitHub
#luminus
<
2018-06-01
>
meguru08:06:39

Hello I want to use luminus + reitit + aleph for websocket application. But I cannot find how to deal with create websocket. So. please let me know a sample program or lecture.

gklijs08:06:08

With compojure you can do something like

(defroutes web-socket-routes
           ;; edit Websocket server endpoint
           (GET "/editable" [:as req]
             (let [ch (ncc/hijack! req true)
                   uid (get-user req)]
               (if (and (is-editor uid) (ncc/websocket-upgrade! ch true))
                 (do
                   (ncc/add-aggregated-listener! ch (* 5 1024 1024)
                                                 {:on-open    (fn [ch] (on-open! ch uid))
                                                  :on-message (fn [ch msg] (on-message! ch uid msg))
                                                  :on-close   (fn [ch reason] (on-close! ch uid reason))
                                                  :on-error   (fn [_ status] (log/warn "error on edit web socket with status" status))})
                   {:status 200 :body ch})
                 (do
                   (log/debug "user with uid" uid "tried to connect to the editable web socket, but doesn't have the rights")
                   (ncc/send-response! ch {:status 403}))))))
In this case I only create a websocket server site if a editor is already logged in, and have a map to define witch functions to call on events.

👍 4
meguru09:06:54

It sounds so great! I'll use it now!