Skip to content

Commit

Permalink
Merge pull request #7 from mvc-works/wss
Browse files Browse the repository at this point in the history
switching to wss behind HTTPS
  • Loading branch information
soyaine authored Feb 14, 2022
2 parents c3ff230 + d5eddfa commit 13f1472
Show file tree
Hide file tree
Showing 6 changed files with 510 additions and 316 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ js-out/
node_modules/
dist/
out-page/

certs/
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ Deps:
yarn add nanoid
```

### WSS

add in options to enabled WSS mode:

```cirru
{}
:key "./path-to.pem"
:cert "./path-to.pem"
```

### License

MIT
201 changes: 175 additions & 26 deletions calcit.cirru

Large diffs are not rendered by default.

24 changes: 20 additions & 4 deletions compact.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{} (:package |ws-edn)
:configs $ {} (:init-fn |ws-edn.app.page/main!) (:reload-fn |ws-edn.app.page/reload!)
:modules $ []
:version |0.0.5
:version |0.0.6-a1
:entries $ {}
:server $ {} (:reload-fn |ws-edn.app.server/reload!) (:storage-key |calcit.cirru)
:modules $ []
Expand Down Expand Up @@ -70,6 +70,8 @@
wss-send! sid $ {} (:op "\"initial message")
:on-data $ fn (sid data) (println "\"just data" sid data)
:on-close $ fn (sid event) (println "\"close" sid)
:key "\"certs/key.pem"
:cert "\"certs/cert.pem"
js/setInterval
fn () (println "\"heartbeat")
wss-each! $ fn (sid socket) (js/console.log sid)
Expand All @@ -84,6 +86,8 @@
ns ws-edn.server $ :require ("\"ws" :as ws)
ws-edn.util :refer $ when-let parse-data
"\"nanoid" :refer $ nanoid
"\"https" :as https
"\"fs" :as fs
:defs $ {}
|wss-each! $ quote
defn wss-each! (handler)
Expand All @@ -105,8 +109,20 @@
defn wss-serve! (port options)
assert "\"first argument is port" $ number? port
let
wss $ new ws/Server
js-object $ "\"port" port
wss $ if
some? $ :cert options
new ws/Server $ let
ssl-options $ js-object
:key $ fs/readFileSync (:key options)
:cert $ fs/readFileSync (:cert options)
server $ https/createServer ssl-options
fn (req res) (.!writeHead res 200) (.!end res "\"WSS Server")
.!addListener server "\"upgrade" $ fn (req res head)
js/console.log $ .-url req
.!on server "\"error" $ fn (err) (js/console.error err)
.!listen server port $ fn () (println "\"server at" port)
js-object (:server server) (:path "\"/")
new ws/Server $ js-object (:port port)
.!on wss "\"connection" $ fn (socket ? req) (maintain-socket! socket options)
.!on wss "\"listening" $ fn ()
let
Expand Down Expand Up @@ -144,7 +160,7 @@
:defs $ {}
|main! $ quote
defn main! () (println "\"start")
ws-connect! "\"ws://localhost:5001" $ {}
ws-connect! "\"wss://localhost:5001" $ {}
:on-open $ fn (event) (println "\"open")
ws-send! $ {} (:op :test)
:on-data $ fn (data) (println "\"data" data)
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": "0.0.5",
"version": "0.0.6-a1",
"dependencies": {
"@calcit/procs": "^0.5.16",
"@calcit/procs": "^0.5.17-a1",
"nanoid": "^3.2.0"
},
"scripts": {
Expand All @@ -14,9 +14,9 @@
"watch-page": "cr --emit-js --emit-path out-page/"
},
"devDependencies": {
"vite": "^2.7.13",
"webpack": "5.67.0",
"vite": "^2.8.1",
"webpack": "5.68.0",
"webpack-cli": "^4.9.2",
"ws": "^8.4.2"
"ws": "^8.5.0"
}
}
Loading

0 comments on commit 13f1472

Please sign in to comment.