This repository has been archived on 2023-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
clj-browserchannel/chat-demo/src/chat_demo/server.clj
Gered f889985fe1 update wrap-browserchannel arguments
passing in event handlers is obviously going to be the most common
usage (all apps won't necessarily need to change the default options,
though many will want to). so it seems silly to hide the event handlers
as an option... i feel like it's better as it's own separate
argument.
2016-05-15 22:16:06 -04:00

65 lines
1.8 KiB
Clojure

(ns chat-demo.server
(:gen-class)
(:require
[compojure.core :refer [routes GET]]
[compojure.route :as route]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
[ring.util.response :refer [response]]
[ring.middleware.anti-forgery :refer [*anti-forgery-token*]]
[clj-pebble.core :as pebble]
[net.thegeez.browserchannel.server :as browserchannel]
[net.thegeez.browserchannel.jetty-async-adapter :as jetty]
[net.thegeez.browserchannel.immutant-async-adapter :as immutant]
[environ.core :refer [env]]))
(def event-handlers
{:on-open
(fn [session-id request]
(println "client" session-id "connected")
(browserchannel/send-data-to-all {:msg (str "client " session-id " connected")}))
:on-close
(fn [session-id request reason]
(println "client" session-id "disconnected. reason: " reason)
(browserchannel/send-data-to-all {:msg (str "client " session-id " disconnected. reason: " reason)}))
:on-receive
(fn [session-id request m]
(println "client" session-id "sent" m)
(browserchannel/send-data-to-all m))})
(def app-routes
(routes
(GET "/" [] (pebble/render-resource
"html/index.html"
{:dev (boolean (env :dev))
:csrfToken *anti-forgery-token*}))
(route/resources "/")
(route/not-found "not found")))
(def handler
(-> app-routes
(browserchannel/wrap-browserchannel event-handlers)
(wrap-defaults site-defaults)))
(defn run-jetty []
(println "Using Jetty adapter")
(jetty/run-jetty
#'handler
{:join? false
:port 8080}))
(defn run-immutant []
(println "Using Immutant adapter")
(immutant/run-immutant
#'handler
{:port 8080}))
(defn -main [& args]
(if (env :dev) (pebble/set-options! :cache false))
;(run-jetty)
(run-immutant)
)