File tree 4 files changed +40
-6
lines changed
4 files changed +40
-6
lines changed Original file line number Diff line number Diff line change @@ -152,7 +152,7 @@ The following configuration keys are supported:
152
152
153
153
- ` :store ` - The Ring session store to use for storing sessions.
154
154
155
- - ` :static `
155
+ - ` :static ` -
156
156
A map of options to configure how to find static content.
157
157
158
158
- ` :files ` -
@@ -168,6 +168,15 @@ The following configuration keys are supported:
168
168
and the rest of the map is passed as options. May also be a
169
169
collection of the above.
170
170
171
+ - ` :websocket `
172
+ A map of options to configure websocket behavior.
173
+
174
+ - ` :keepalive ` -
175
+ If true, periodically pings the client to keep the connection
176
+ alive via the [ websocket keepalive] [ 16 ] middleware. A map of
177
+ options may also be passed to set the ` :period ` of the keepalive in
178
+ milliseconds.
179
+
171
180
172
181
[ 1 ] : https://ring-clojure.github.io/ring/ring.middleware.multipart-params.html
173
182
[ 2 ] : https://ring-clojure.github.io/ring/ring.middleware.nested-params.html
@@ -184,6 +193,7 @@ The following configuration keys are supported:
184
193
[ 13 ] : https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection
185
194
[ 14 ] : https://ring-clojure.github.io/ring/ring.middleware.file.html
186
195
[ 15 ] : https://ring-clojure.github.io/ring/ring.middleware.resource.html
196
+ [ 16 ] : https://ring-clojure.github.io/ring-websocket-middleware/ring.websocket.keepalive.html
187
197
188
198
## License
189
199
Original file line number Diff line number Diff line change 7
7
[ring/ring-core " 1.13.0" ]
8
8
[ring/ring-ssl " 0.4.0" ]
9
9
[ring/ring-headers " 0.4.0" ]
10
- [ring/ring-anti-forgery " 1.4.0" ]]
10
+ [ring/ring-anti-forgery " 1.4.0" ]
11
+ [org.ring-clojure/ring-websocket-middleware " 0.2.1" ]]
11
12
:aliases
12
13
{" test-all" [" with-profile" " default:+1.10:+1.11:+1.12" " test" ]}
13
14
:profiles
Original file line number Diff line number Diff line change 17
17
[ring.middleware.default-charset :refer [wrap-default-charset]]
18
18
[ring.middleware.absolute-redirects :refer [wrap-absolute-redirects]]
19
19
[ring.middleware.ssl :refer [wrap-ssl-redirect wrap-hsts wrap-forwarded-scheme]]
20
- [ring.middleware.proxy-headers :refer [wrap-forwarded-remote-addr]]))
20
+ [ring.middleware.proxy-headers :refer [wrap-forwarded-remote-addr]]
21
+ [ring.websocket.keepalive :refer [wrap-websocket-keepalive]]))
21
22
22
23
(def default-session-store (cookie-store ))
23
24
54
55
:responses {:not-modified-responses true
55
56
:absolute-redirects false
56
57
:content-types true
57
- :default-charset " utf-8" }})
58
+ :default-charset " utf-8" }
59
+ :websocket {:keepalive true }})
58
60
59
61
(def secure-site-defaults
60
62
" A default configuration for a browser-accessible website that's accessed
103
105
secure-site-defaults"
104
106
[handler config]
105
107
(-> handler
108
+ (wrap wrap-websocket-keepalive (get-in config [:websocket :keepalive ] false ))
106
109
(wrap wrap-anti-forgery (get-in config [:security :anti-forgery ] false ))
107
110
(wrap wrap-flash (get-in config [:session :flash ] false ))
108
111
(wrap wrap-session (:session config false ))
Original file line number Diff line number Diff line change 2
2
(:require [clojure.test :refer :all ]
3
3
[ring.middleware.defaults :refer :all ]
4
4
[ring.util.response :refer [response content-type not-found]]
5
- [ring.mock.request :refer [request header]]))
5
+ [ring.mock.request :refer [request header]]
6
+ [ring.websocket :as ws]
7
+ [ring.websocket.protocols :as wsp]))
6
8
7
9
(deftest test-wrap-defaults
8
10
(testing " api defaults"
248
250
(wrap-defaults site-defaults))]
249
251
(is (= 403 (:status (handler (request :post " /" )))))
250
252
(is (= 200 (:status (handler (-> (request :post " /" )
251
- (header " X-Ring-Anti-Forgery" " 1" )))))))))
253
+ (header " X-Ring-Anti-Forgery" " 1" ))))))))
254
+
255
+ (testing " websocket pings"
256
+ (let [ping-count (atom 0 )
257
+ socket (reify wsp/Socket
258
+ (-open? [_] true )
259
+ (-send [_ _])
260
+ (-ping [_ _] (swap! ping-count inc))
261
+ (-pong [_ _])
262
+ (-close [_ _ _]))
263
+ response {::ws/listener {}}
264
+ handler (wrap-defaults (constantly response)
265
+ {:websocket {:keepalive {:period 10 }}})
266
+ listener (::ws/listener (handler {}))]
267
+ (wsp/on-open listener socket)
268
+ (Thread/sleep 41 )
269
+ (wsp/on-close listener socket 1000 " " )
270
+ (Thread/sleep 20 )
271
+ (is (= 4 @ping-count)))))
You can’t perform that action at this time.
0 commit comments