(defprotocol Channel
"Unified asynchronous channel interface for HTTP (streaming or long-polling)
and WebSocket."
(open? [ch] "Returns true iff channel is open.")
(close [ch]
"Closes the channel. Idempotent: returns true if the channel was actually
closed, or false if it was already closed.")
(websocket? [ch] "Returns true iff channel is a WebSocket.")
(send! [ch data] [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")
(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[].")
(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)"))