@@ -15,9 +15,8 @@ extern crate time;
1515extern crate iron;
1616#[ cfg( feature = "serve" ) ]
1717extern crate staticfile;
18-
1918#[ cfg( feature = "serve" ) ]
20- mod livereload ;
19+ extern crate ws ;
2120
2221use std:: env;
2322use std:: error:: Error ;
@@ -32,10 +31,6 @@ use notify::Watcher;
3231#[ cfg( feature = "watch" ) ]
3332use std:: sync:: mpsc:: channel;
3433
35- // Uses for the Serve feature
36- #[ cfg( feature = "serve" ) ]
37- use livereload:: LiveReload ;
38-
3934
4035use mdbook:: MDBook ;
4136
@@ -187,16 +182,21 @@ fn watch(args: &ArgMatches) -> Result<(), Box<Error>> {
187182// Watch command implementation
188183#[ cfg( feature = "serve" ) ]
189184fn serve ( args : & ArgMatches ) -> Result < ( ) , Box < Error > > {
185+ const RELOAD_COMMAND : & ' static str = "reload" ;
186+
190187 let book_dir = get_book_dir ( args) ;
191188 let mut book = MDBook :: new ( & book_dir) . read_config ( ) ;
192189 let port = args. value_of ( "port" ) . unwrap_or ( "3000" ) ;
193190 let ws_port = args. value_of ( "ws-port" ) . unwrap_or ( "3001" ) ;
194191
192+ let address = format ! ( "localhost:{}" , port) ;
193+ let ws_address = format ! ( "localhost:{}" , ws_port) ;
194+
195195 book. set_livereload ( format ! ( r#"
196196 <script type="text/javascript">
197- var socket = new WebSocket("ws://localhost:{}", "livereload" );
197+ var socket = new WebSocket("ws://localhost:{}");
198198 socket.onmessage = function (event) {{
199- if (event.data === "reload ") {{
199+ if (event.data === "{} ") {{
200200 socket.close();
201201 location.reload(true); // force reload from server (not from cache)
202202 }}
@@ -206,23 +206,32 @@ fn serve(args: &ArgMatches) -> Result<(), Box<Error>> {
206206 socket.close();
207207 }}
208208 </script>
209- "# , ws_port) . to_owned ( ) ) ;
209+ "# , ws_port, RELOAD_COMMAND ) . to_owned ( ) ) ;
210210
211211 try!( book. build ( ) ) ;
212212
213213 let staticfile = staticfile:: Static :: new ( book. get_dest ( ) ) ;
214214 let iron = iron:: Iron :: new ( staticfile) ;
215- let _iron = iron. http ( & * format ! ( "localhost:{}" , port ) ) . unwrap ( ) ;
215+ let _iron = iron. http ( & * address ) . unwrap ( ) ;
216216
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+ } ) ;
218228
219- println ! ( "{:?}" , "Registering change trigger" ) ;
220229 trigger_on_change ( & mut book, move |event, book| {
221230 if let Some ( path) = event. path {
222231 println ! ( "File changed: {:?}\n Building book...\n " , path) ;
223232 match book. build ( ) {
224233 Err ( e) => println ! ( "Error while building: {:?}" , e) ,
225- _ => lr . trigger_reload ( ) ,
234+ _ => broadcaster . send ( RELOAD_COMMAND ) . unwrap ( ) ,
226235 }
227236 println ! ( "" ) ;
228237 }
@@ -282,6 +291,8 @@ fn trigger_on_change<F>(book: &mut MDBook, closure: F) -> ()
282291
283292 let mut previous_time = time:: get_time ( ) ;
284293
294+ println ! ( "\n Listening for changes...\n " ) ;
295+
285296 loop {
286297 match rx. recv ( ) {
287298 Ok ( event) => {
0 commit comments