-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from celiocidral/websocket-protocol-upgrade-su…
…pport Add support for websocket protocol upgrade.
- Loading branch information
Showing
4 changed files
with
187 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
(ns rj9a.websocket-upgrade | ||
(:gen-class) | ||
(:require [ring.adapter.jetty9 :as jetty] | ||
[ring.adapter.jetty9.websocket :refer [ws-upgrade-request? ws-upgrade-response]])) | ||
|
||
(defn my-websocket-handler [_] | ||
{:on-connect (fn on-connect [_] | ||
(tap> [:ws :connect])) | ||
:on-text (fn on-text [ws text-message] | ||
(tap> [:ws :msg text-message]) | ||
(jetty/send! ws (str "echo: " text-message))) | ||
:on-bytes (fn on-bytes [_ _ _ _] | ||
(tap> [:ws :bytes])) | ||
:on-close (fn on-close [_ status-code reason] | ||
(tap> [:ws :close status-code reason])) | ||
:on-ping (fn on-ping [ws payload] | ||
(tap> [:ws :ping]) | ||
(jetty/send! ws payload)) | ||
:on-pong (fn on-pong [_ _] | ||
(tap> [:ws :pong])) | ||
:on-error (fn on-error [_ e] | ||
(tap> [:ws :error e]))}) | ||
|
||
(defn handler [req] | ||
(if (ws-upgrade-request? req) | ||
(ws-upgrade-response my-websocket-handler) | ||
{:status 200 :body "hello"})) | ||
|
||
(defn async-handler [request send-response _] | ||
(send-response | ||
(if (ws-upgrade-request? request) | ||
(ws-upgrade-response my-websocket-handler) | ||
{:status 200 :body "hello"}))) | ||
|
||
(defonce server (atom nil)) | ||
|
||
(defn start! [async?] | ||
(when-not @server | ||
(reset! server (jetty/run-jetty | ||
(if async? #'async-handler #'handler) | ||
{:port 5000 | ||
:join? false | ||
:async? async? | ||
:allow-null-path-info true | ||
;; The same ws can also be available via the old regular websocket endpoints. | ||
;; It's added here in this example just for regression testing purposes. | ||
:websockets {"/mywebsocket" my-websocket-handler}})))) | ||
|
||
(defn stop! [] | ||
(when @server | ||
(jetty/stop-server @server) | ||
(reset! server nil))) | ||
|
||
(comment | ||
(start! false) | ||
(stop!)) | ||
|
||
(defn -main [& _] | ||
(start! false)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters