-
Notifications
You must be signed in to change notification settings - Fork 1
/
websockets.clj
168 lines (145 loc) · 5.84 KB
/
websockets.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
(ns slipway.websockets
(:require [slipway.common.websockets :as common.ws]
[slipway.servlet :as servlet])
(:import (clojure.lang IFn)
(java.nio ByteBuffer)
(java.time Duration)
(org.eclipse.jetty.websocket.api RemoteEndpoint Session WebSocketAdapter WebSocketPingPongListener WriteCallback)
(org.eclipse.jetty.websocket.server JettyServerUpgradeRequest JettyWebSocketCreator JettyWebSocketServerContainer)))
(def no-op (constantly nil))
(extend-protocol servlet/RequestMapDecoder
JettyServerUpgradeRequest
(build-request-map [request]
(assoc (-> (.getHttpServletRequest request) (servlet/updgrade-servlet-request-map))
:websocket-subprotocols (into [] (.getSubProtocols request))
:websocket-extensions (into [] (.getExtensions request)))))
(defn write-callback
[{:keys [write-failed write-success]
:or {write-failed no-op
write-success no-op}}]
(reify WriteCallback
(writeFailed [_ throwable]
(write-failed throwable))
(writeSuccess [_]
(write-success))))
(extend-protocol common.ws/WebSocketSend
(Class/forName "[B")
(-send!
([ba ws]
(common.ws/-send! (ByteBuffer/wrap ba) ws))
([ba ws callback]
(common.ws/-send! (ByteBuffer/wrap ba) ws callback))))
(extend-protocol common.ws/WebSocketSend
ByteBuffer
(-send!
([bb ws]
(-> ^WebSocketAdapter ws .getRemote (.sendBytes ^ByteBuffer bb)))
([bb ws callback]
(-> ^WebSocketAdapter ws .getRemote (.sendBytes ^ByteBuffer bb ^WriteCallback (write-callback callback)))))
String
(-send!
([s ws]
(-> ^WebSocketAdapter ws .getRemote (.sendString ^String s)))
([s ws callback]
(-> ^WebSocketAdapter ws .getRemote (.sendString ^String s ^WriteCallback (write-callback callback)))))
IFn
(-send! [f ws]
(-> ^WebSocketAdapter ws .getRemote f))
Object
(send!
([this ws]
(-> ^WebSocketAdapter ws .getRemote
(.sendString ^RemoteEndpoint (str this))))
([this ws callback]
(-> ^WebSocketAdapter ws .getRemote
(.sendString ^RemoteEndpoint (str this) ^WriteCallback (write-callback callback))))))
(extend-protocol common.ws/WebSocketPing
(Class/forName "[B")
(-ping! [ba ws] (common.ws/-ping! (ByteBuffer/wrap ba) ws)))
(extend-protocol common.ws/WebSocketPing
ByteBuffer
(-ping! [bb ws] (-> ^WebSocketAdapter ws .getRemote (.sendPing ^ByteBuffer bb)))
String
(-ping! [s ws] (common.ws/-ping! (.getBytes ^String s "UTF-8") ws))
Object
(-ping! [o ws] (common.ws/-ping! (.getBytes (str o) "UTF-8") ws)))
(extend-protocol common.ws/WebSockets
WebSocketAdapter
(send!
([this msg]
(common.ws/-send! msg this))
([this msg callback]
(common.ws/-send! msg this callback)))
(ping!
([this]
(common.ws/-ping! (ByteBuffer/allocate 0) this))
([this msg]
(common.ws/-ping! msg this)))
(close!
([this]
(.close (.getSession this)))
([this status-code reason]
(.close (.getSession this) status-code reason)))
(remote-addr [this]
(.getRemoteAddress (.getSession this)))
(idle-timeout! [this ms]
(.setIdleTimeout (.getSession this) ms))
(connected? [this]
(. this (isConnected)))
(req-of [this]
(servlet/build-request-map (.getUpgradeResponse (.getSession this)))))
(defn proxy-ws-adapter
[{:keys [on-connect on-error on-text on-close on-bytes on-ping on-pong]
:or {on-connect no-op
on-error no-op
on-text no-op
on-close no-op
on-bytes no-op
on-ping no-op
on-pong no-op}}]
(proxy [WebSocketAdapter WebSocketPingPongListener] []
(onWebSocketConnect [^Session session]
(let [^WebSocketAdapter _ this]
(proxy-super onWebSocketConnect session))
(on-connect this))
(onWebSocketError [^Throwable e]
(on-error this e))
(onWebSocketText [^String message]
(on-text this message))
(onWebSocketClose [statusCode ^String reason]
(let [^WebSocketAdapter _ this]
(proxy-super onWebSocketClose statusCode reason))
(on-close this statusCode reason))
(onWebSocketBinary [^bytes payload offset len]
(on-bytes this payload offset len))
(onWebSocketPing [^ByteBuffer bytebuffer]
(on-ping this bytebuffer))
(onWebSocketPong [^ByteBuffer bytebuffer]
(on-pong this bytebuffer))))
(defn reify-ws-creator
[ws-fns]
(reify JettyWebSocketCreator
(createWebSocket [_ _ _]
(proxy-ws-adapter ws-fns))))
(comment
#:slipway.websockets {:idle-timeout "max websocket idle time (in ms)"
:input-buffer-size "max websocket input buffer size"
:output-buffer-size "max websocket output buffer size"
:max-text-message-size "max websocket text message size"
:max-binary-message-size "max websocket binary message size"
:max-frame-size "max websoccket frame size"
:auto-fragment "websocket auto fragment"})
(defn upgrade-websocket
[req res ws opts]
(let [{::keys [idle-timeout input-buffer-size output-buffer-size max-text-message-size
max-binary-message-size max-frame-size auto-fragment]} opts
creator (reify-ws-creator ws)
container (JettyWebSocketServerContainer/getContainer (servlet/get-context req))]
(some->> idle-timeout (Duration/ofMillis) (.setIdleTimeout container))
(some->> input-buffer-size (.setInputBufferSize container))
(some->> output-buffer-size (.setOutputBufferSize container))
(some->> max-text-message-size (.setMaxTextMessageSize container))
(some->> max-binary-message-size (.setMaxBinaryMessageSize container))
(some->> max-frame-size (.setMaxFrameSize container))
(some->> auto-fragment (.setAutoFragment container))
(.upgrade container creator req res)))