-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The code is adapted from dream-livereload, itself adapted from the w-live-reload example. We update the w-live-reload to use the newly introduced middleware.
- Loading branch information
Showing
7 changed files
with
103 additions
and
143 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
dream.cipher | ||
dream-pure | ||
fmt | ||
lambdasoup | ||
logs | ||
lwt | ||
magic-mime | ||
|
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,78 @@ | ||
(* This file is part of Dream, released under the MIT license. See LICENSE.md | ||
for details, or visit https://github.com/aantron/dream. | ||
Copyright 2021 Anton Bachin *) | ||
|
||
module Message = Dream_pure.Message | ||
|
||
let default_script ?(retry_interval_ms = 500) ?(max_retry_ms = 5000) | ||
?(route = "/_livereload") () = | ||
Printf.sprintf | ||
{js| | ||
var socketUrl = "ws://" + location.host + "%s" | ||
var s = new WebSocket(socketUrl); | ||
|
||
s.onopen = function(even) { | ||
console.log("WebSocket connection open."); | ||
}; | ||
|
||
s.onclose = function(even) { | ||
console.log("WebSocket connection closed."); | ||
const innerMs = %i; | ||
const maxMs = %i; | ||
const maxAttempts = Math.round(maxMs / innerMs); | ||
let attempts = 0; | ||
function reload() { | ||
attempts++; | ||
if(attempts > maxAttempts) { | ||
console.error("Could not reconnect to dev server."); | ||
return; | ||
} | ||
|
||
s2 = new WebSocket(socketUrl); | ||
|
||
s2.onerror = function(event) { | ||
setTimeout(reload, innerMs); | ||
}; | ||
|
||
s2.onopen = function(event) { | ||
location.reload(); | ||
}; | ||
}; | ||
reload(); | ||
}; | ||
|
||
s.onerror = function(event) { | ||
console.error("WebSocket error observed:", event); | ||
}; | ||
|js} | ||
route retry_interval_ms max_retry_ms | ||
|
||
let livereload ?(script = default_script ()) ?(path = "/_livereload") () | ||
(next_handler : Message.request -> Message.response Lwt.t) | ||
(request : Message.request) : Message.response Lwt.t = | ||
match Message.target request with | ||
| target when target = path -> | ||
Helpers.websocket (fun socket -> | ||
Lwt.bind (Helpers.receive socket) (fun _ -> | ||
Message.close_websocket socket)) | ||
| _ -> ( | ||
let%lwt response = next_handler request in | ||
match Message.header response "Content-Type" with | ||
| Some "text/html" | Some "text/html; charset=utf-8" -> ( | ||
let%lwt body = Message.body response in | ||
let soup = | ||
Markup.string body | ||
|> Markup.parse_html ~context:`Document | ||
|> Markup.signals | ||
|> Soup.from_signals | ||
in | ||
let open Soup.Infix in | ||
match soup $? "head" with | ||
| None -> Lwt.return response | ||
| Some head -> | ||
Soup.create_element "script" ~inner_text:script | ||
|> Soup.append_child head; | ||
Message.set_body response (Soup.to_string soup); | ||
Lwt.return response) | ||
| _ -> Lwt.return response) |