forked from ring-clojure/ring
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.clj
45 lines (37 loc) · 920 Bytes
/
example.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(ns ring.adapter.example
(:require
ring.adapter.jetty
ring.websocket))
(defn wrap-quote
"Quote the message"
[handler]
(fn [request]
(update-in (handler request) [:body] (fn [m] (str \" m \")))))
;; A simple echo channel
(def echo (ring.websocket/simple-channel
(wrap-quote (fn [request] request))))
(def app
(fn [request]
(if (= :websocket-connect (:request-method request))
{:websocket echo})))
;;; Functions to start and stop jetty
(defonce server (atom nil))
(defn- run
"Run app in jetty"
[app options]
(ring.adapter.jetty/run-jetty app options))
(defn start
"Start the app, keeping track of the server"
[]
(do
(reset!
server
(run
app
{:port 8081
:join? false
:proxy-handler ring.adapter.jetty/websocket-proxy-handler}))))
(defn stop
"Stop the app"
[]
(swap! server (fn [server] (.stop server) nil)))