-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added wrapper for nodejs websocket server. #55
base: master
Are you sure you want to change the base?
Changes from 1 commit
26d8f72
8c28a85
2264c9d
b736df8
9128c13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
(ns chord.node | ||
(:require [cljs.nodejs :as nodejs] | ||
[chord.channels :as channels] | ||
[chord.format :as format])) | ||
|
||
(defn ws-server [ws-opts {:keys [read-ch write-ch :as chord-opts]} handler] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe a more consistent signature here is |
||
(when-let [ws (nodejs/require "ws")] | ||
(let [server (ws.Server. (clj->js ws-opts))] | ||
(.on server "connection" | ||
(fn [socket request] | ||
(let [ws-ch (channels/wrap-websocket socket chord-opts #(.close socket))] | ||
(handler ws-ch request)))) | ||
server))) | ||
|
||
(comment | ||
"Plain example" | ||
|
||
(defn handler [ws-ch req] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are great examples - could you also put them in the README to make them more discoverable? |
||
(if (authorized? req) | ||
(async/put! ws-ch "Welcome!") | ||
(do | ||
(async/put! ws-ch "Denied!") | ||
(async/close! ws-ch))) | ||
(async/go | ||
(println (async/<! ws-ch)) | ||
'...)) | ||
|
||
(def server (chord.node/ws-server {:port 8080} {:format :edn}) handler) | ||
|
||
"Express Example" | ||
|
||
(def express (nodejs/require "express")) | ||
(def https (nodejs/require "https")) | ||
(def app (express)) | ||
'... | ||
|
||
;; Note: SSL is inherited from the webserver, so using http gives you ws, and | ||
;; https gives you wss. | ||
(def server (.createServer https app)) | ||
|
||
(def ws-server | ||
(chord.node/ws-server server {:format :json} handler))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you coalesce the opts map and the cleanup function, so that we don't have multiple options parameters? e.g.
(defn wrap-websocket [socket {:keys [read-ch write-ch on-close] :as opts}] ...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After a bit more thought, I wonder why we leave this up to the implementer at all? We only have two types of connections, so I can clean up automatically.