org.httpkit.server
Channel
Unified asynchronous channel interface for HTTP (streaming or long-polling)
and WebSocket.
accept
DEPRECATED for `sec-websocket-accept
(close ch)
Closes the channel. Idempotent: returns true if the channel was actually
closed, or false if it was already closed.
(on-close ch callback)
Sets handler (fn [status]) for notification of channel being closed by the
server or client. Handler will be invoked at most once. Useful for clean-up.
Callback status argument:
:server-close : Channel closed by sever
:client-close : HTTP channel closed by client
:normal : WebSocket closed by client (CLOSE_NORMAL)
:going-away : WebSocket closed by client (CLOSE_GOING_AWAY)
:protocol-error : WebSocket closed by client (CLOSE_PROTOCOL_ERROR)
:unsupported : WebSocket closed by client (CLOSE_UNSUPPORTED)
:unknown : WebSocket closed by client (unknown reason)
(on-receive ch callback)
Sets handler (fn [message]) for notification of client WebSocket
messages. Message ordering is guaranteed by server.
The message argument could be a string or a byte[].
(run-server handler & [{:keys [ip port thread queue-size max-body max-ws max-line proxy-protocol worker-name-prefix], :or {max-ws 4194304, max-body 8388608, max-line 4096, ip "0.0.0.0", queue-size 20480, proxy-protocol :disable, port 8090, thread 4, worker-name-prefix "worker-"}}])
Starts HTTP server and returns
(fn [& {:keys [timeout] ; Timeout (msecs) to wait on existing reqs to complete
:or {timeout 100}}])
Server is mostly Ring compatible, see http://http-kit.org/migration.html
for differences.
Options:
:ip ; Which ip (if has many ips) to bind
:port ; Which port listen incomming request
:thread ; Http worker thread count
:queue-size ; Max job queued before reject to project self
:max-body ; Max http body: 8m
:max-ws ; Max websocket message size
:max-line ; Max http inital line length
:proxy-protocol ; Proxy protocol e/o #{:disable :enable :optional}
:worker-name-prefix ; Woker thread name prefix
(send! ch data)
(send! ch data close-after-send?)
Sends data to client and returns true if the data was successfully sent,
or false if the channel is closed. Data is sent directly to the client,
NO RING MIDDLEWARE IS APPLIED.
When unspecified, `close-after-send?` defaults to true for HTTP channels
and false for WebSocket.
Data form: {:headers _ :status _ :body _} or just body. Note that :headers
and :status will be stripped for WebSocket and for HTTP streaming responses
after the first.
For WebSocket, a text frame is sent to client if data is String,
a binary frame when data is byte[] or InputStream. For for HTTP streaming
responses, data can be one of the type defined by Ring spec
(with-channel ring-req ch-name & body)
Evaluates body with `ch-name` bound to the request's underlying
asynchronous HTTP or WebSocket channel, and returns {:body AsyncChannel}
as an implementation detail.
;; Asynchronous HTTP response (with optional streaming)
(defn my-async-handler [request]
(with-channel request ch ; Request's channel
;; Make ch available to whoever can deliver the response to it; ex.:
(swap! clients conj ch))) ; given (def clients (atom #{}))
;; Some place later:
(doseq [ch @clients]
(swap! clients disj ch)
(send! ch {:status 200
:headers {"Content-Type" "text/html"}
:body your-async-response}
;; false ; Uncomment to use chunk encoding for HTTP streaming
)))
;; WebSocket response
(defn my-chatroom-handler [request]
(if-not (:websocket? request)
{:status 200 :body "Welcome to the chatroom! JS client connecting..."}
(with-channel request ch
(println "New WebSocket channel:" ch)
(on-receive ch (fn [msg] (println "on-receive:" msg)))
(on-close ch (fn [status] (println "on-close:" status)))
(send! ch "First chat message!"))))
Channel API (see relevant docstrings for more info):
(open? [ch])
(close [ch])
(websocket? [ch])
(send! [ch data] [ch data close-after-send?])
(on-receieve [ch callback])
(on-close [ch callback])
See org.httpkit.timer ns for optional timeout facilities.