@@ -15,9 +15,8 @@ extern crate time;
15
15
extern crate iron;
16
16
#[ cfg( feature = "serve" ) ]
17
17
extern crate staticfile;
18
-
19
18
#[ cfg( feature = "serve" ) ]
20
- mod livereload ;
19
+ extern crate ws ;
21
20
22
21
use std:: env;
23
22
use std:: error:: Error ;
@@ -32,10 +31,6 @@ use notify::Watcher;
32
31
#[ cfg( feature = "watch" ) ]
33
32
use std:: sync:: mpsc:: channel;
34
33
35
- // Uses for the Serve feature
36
- #[ cfg( feature = "serve" ) ]
37
- use livereload:: LiveReload ;
38
-
39
34
40
35
use mdbook:: MDBook ;
41
36
@@ -187,16 +182,21 @@ fn watch(args: &ArgMatches) -> Result<(), Box<Error>> {
187
182
// Watch command implementation
188
183
#[ cfg( feature = "serve" ) ]
189
184
fn serve ( args : & ArgMatches ) -> Result < ( ) , Box < Error > > {
185
+ const RELOAD_COMMAND : & ' static str = "reload" ;
186
+
190
187
let book_dir = get_book_dir ( args) ;
191
188
let mut book = MDBook :: new ( & book_dir) . read_config ( ) ;
192
189
let port = args. value_of ( "port" ) . unwrap_or ( "3000" ) ;
193
190
let ws_port = args. value_of ( "ws-port" ) . unwrap_or ( "3001" ) ;
194
191
192
+ let address = format ! ( "localhost:{}" , port) ;
193
+ let ws_address = format ! ( "localhost:{}" , ws_port) ;
194
+
195
195
book. set_livereload ( format ! ( r#"
196
196
<script type="text/javascript">
197
- var socket = new WebSocket("ws://localhost:{}", "livereload" );
197
+ var socket = new WebSocket("ws://localhost:{}");
198
198
socket.onmessage = function (event) {{
199
- if (event.data === "reload ") {{
199
+ if (event.data === "{} ") {{
200
200
socket.close();
201
201
location.reload(true); // force reload from server (not from cache)
202
202
}}
@@ -206,23 +206,32 @@ fn serve(args: &ArgMatches) -> Result<(), Box<Error>> {
206
206
socket.close();
207
207
}}
208
208
</script>
209
- "# , ws_port) . to_owned ( ) ) ;
209
+ "# , ws_port, RELOAD_COMMAND ) . to_owned ( ) ) ;
210
210
211
211
try!( book. build ( ) ) ;
212
212
213
213
let staticfile = staticfile:: Static :: new ( book. get_dest ( ) ) ;
214
214
let iron = iron:: Iron :: new ( staticfile) ;
215
- let _iron = iron. http ( & * format ! ( "localhost:{}" , port ) ) . unwrap ( ) ;
215
+ let _iron = iron. http ( & * address ) . unwrap ( ) ;
216
216
217
- let lr = LiveReload :: new ( & format ! ( "localhost:{}" , ws_port) ) . unwrap ( ) ;
217
+ let ws_server = ws:: WebSocket :: new ( |_| {
218
+ |_| {
219
+ Ok ( ( ) )
220
+ }
221
+ } ) . unwrap ( ) ;
222
+
223
+ let broadcaster = ws_server. broadcaster ( ) ;
224
+
225
+ std:: thread:: spawn ( move || {
226
+ ws_server. listen ( & * ws_address) . unwrap ( ) ;
227
+ } ) ;
218
228
219
- println ! ( "{:?}" , "Registering change trigger" ) ;
220
229
trigger_on_change ( & mut book, move |event, book| {
221
230
if let Some ( path) = event. path {
222
231
println ! ( "File changed: {:?}\n Building book...\n " , path) ;
223
232
match book. build ( ) {
224
233
Err ( e) => println ! ( "Error while building: {:?}" , e) ,
225
- _ => lr . trigger_reload ( ) ,
234
+ _ => broadcaster . send ( RELOAD_COMMAND ) . unwrap ( ) ,
226
235
}
227
236
println ! ( "" ) ;
228
237
}
@@ -282,6 +291,8 @@ fn trigger_on_change<F>(book: &mut MDBook, closure: F) -> ()
282
291
283
292
let mut previous_time = time:: get_time ( ) ;
284
293
294
+ println ! ( "\n Listening for changes...\n " ) ;
295
+
285
296
loop {
286
297
match rx. recv ( ) {
287
298
Ok ( event) => {
0 commit comments