### Invoking a Ring Adapter to Start an HTTP Server in Clojure Source: https://github.com/ring-clojure/ring/blob/master/SPEC.md This code demonstrates how to invoke a Ring adapter. An adapter is a side-effectful function that takes a `handler` function and an `options` map, then starts an HTTP server. It processes incoming requests, converts them to request maps, passes them to the handler, and sends the handler's response back to the client. ```Clojure (run-adapter handler options) ``` -------------------------------- ### Running Ring Adapter in Asynchronous Mode Source: https://github.com/ring-clojure/ring/blob/master/SPEC.md This example shows how to invoke a Ring adapter, such as `run-adapter`, with an option to specify asynchronous behavior. The `:async? true` option indicates that the adapter should use the asynchronous handler API. ```Clojure (run-adapter handler {:async? true}) ``` -------------------------------- ### Defining Ring Websocket PingListener Protocol Source: https://github.com/ring-clojure/ring/blob/master/SPEC.md This optional protocol defines the `on-ping` callback for a websocket listener. If implemented, the user is responsible for sending the corresponding 'pong' message. If not implemented, the adapter will automatically respond to ping messages with a pong. ```Clojure (defprotocol PingListener (on-ping [listener socket data])) ``` -------------------------------- ### Adding Ring Core Dependency to deps.edn (Clojure) Source: https://github.com/ring-clojure/ring/blob/master/README.md This snippet shows how to add the `ring-core` library as a dependency to a Clojure project using `deps.edn`. It specifies the library group and artifact ID, along with its Maven version. This allows the project to use Ring's core functions and middleware. ```Clojure ring/ring-core {:mvn/version "1.14.1"} ``` -------------------------------- ### Adding Ring Core Dependency to Leiningen (Clojure) Source: https://github.com/ring-clojure/ring/blob/master/README.md This snippet demonstrates how to declare the `ring-core` library as a dependency in a Leiningen project file. It includes the library's group, artifact ID, and version, enabling the project to utilize Ring's core functionalities. ```Clojure [ring/ring-core "1.14.1"] ``` -------------------------------- ### Defining Ring Websocket Listener Protocol Source: https://github.com/ring-clojure/ring/blob/master/SPEC.md This protocol defines the essential callbacks for a Ring websocket listener. It includes functions for handling websocket events such as opening (`on-open`), receiving messages (`on-message`), errors (`on-error`), and closing (`on-close`). The `on-pong` callback is also part of this protocol. ```Clojure (defprotocol Listener (on-open [listener socket]) (on-message [listener socket message]) (on-pong [listener socket data]) (on-error [listener socket throwable]) (on-close [listener socket code reason])) ``` -------------------------------- ### Asynchronous Ring Handler - Exception Handling Source: https://github.com/ring-clojure/ring/blob/master/SPEC.md This asynchronous handler function illustrates how to handle and raise an exception. It takes a `request` map, a `respond` callback, and a `raise` callback. The `raise` function is invoked with the `exception` object to signal an error. ```Clojure (fn [request respond raise] (raise exception)) ``` -------------------------------- ### Returning Websocket Response from Asynchronous Handler Source: https://github.com/ring-clojure/ring/blob/master/SPEC.md This asynchronous Ring handler shows how to send a websocket response using the `respond` callback. Similar to the synchronous version, it provides a map with the `:ring.websocket/listener` key, indicating a websocket upgrade. ```Clojure (fn [request respond raise] (respond #:ring.websocket{:listener websocket-listener})) ``` -------------------------------- ### Ring Handler Supporting Synchronous and Asynchronous Behavior Source: https://github.com/ring-clojure/ring/blob/master/SPEC.md This handler function demonstrates how to support both synchronous and asynchronous API calls by defining multiple arities. The single-argument arity handles synchronous requests, while the three-argument arity handles asynchronous requests by invoking the `respond` callback. ```Clojure (fn ([request] response) ([request respond raise] (respond response))) ``` -------------------------------- ### Asynchronous Ring Handler - Successful Response Source: https://github.com/ring-clojure/ring/blob/master/SPEC.md This asynchronous handler function demonstrates how to send a successful HTTP response. It takes a `request` map, a `respond` callback for sending the response, and a `raise` callback for exceptions. The `respond` function is invoked with the `response` map. ```Clojure (fn [request respond raise] (respond response)) ``` -------------------------------- ### Returning Websocket Response from Synchronous Handler Source: https://github.com/ring-clojure/ring/blob/master/SPEC.md This synchronous Ring handler demonstrates how to return a websocket response instead of a standard HTTP response. It constructs a map with the `:ring.websocket/listener` key, which must satisfy the `ring.websocket/Listener` protocol. ```Clojure (fn [request] #:ring.websocket{:listener websocket-listener}) ``` -------------------------------- ### Defining the Ring Clojure Websocket AsyncSocket Protocol Source: https://github.com/ring-clojure/ring/blob/master/SPEC.md This Clojure code defines the `ring.websocket.protocols/AsyncSocket` protocol, an optional extension for websocket sockets in Ring. It provides an asynchronous method for sending messages, allowing for non-blocking operations and callback functions for success and failure, which is useful for performance-critical applications. ```Clojure (defprotocol AsyncSocket (-send-async [socket message succeed fail])) ``` -------------------------------- ### Defining the Ring Clojure Websocket Socket Protocol Source: https://github.com/ring-clojure/ring/blob/master/SPEC.md This Clojure code defines the `ring.websocket.protocols/Socket` protocol, which is mandatory for any object acting as a websocket socket in Ring. It specifies the fundamental operations for managing a websocket connection, including checking its open status, sending messages, sending ping/pong frames, and closing the connection with a specific code and reason. ```Clojure (defprotocol Socket (-open? [socket]) (-send [socket message]) (-ping [socket data]) (-pong [socket data]) (-close [socket code reason])) ``` -------------------------------- ### Defining a Synchronous Ring Handler in Clojure Source: https://github.com/ring-clojure/ring/blob/master/SPEC.md This snippet illustrates the basic structure of a synchronous Ring handler. A handler is a Clojure function that accepts a single `request` map as an argument and is expected to return a `response` map, forming the core logic of a web application. ```Clojure (fn [request] response) ``` -------------------------------- ### Defining StreamableResponseBody Protocol in Ring Source: https://github.com/ring-clojure/ring/blob/master/SPEC.md This protocol defines the interface for response bodies that can be streamed to an output stream. It specifies the `write-body-to-stream` function, which takes the body, response, and output stream as arguments to write the body content. ```Clojure (defprotocol StreamableResponseBody (write-body-to-stream [body response output-stream])) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.