Skip to content

Commit c805129

Browse files
committed
Switch from rust-websocket to ws-rs
1 parent 2f43167 commit c805129

File tree

3 files changed

+26
-225
lines changed

3 files changed

+26
-225
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ crossbeam = { version = "0.2.8", optional = true }
2828
# Serve feature
2929
iron = { version = "0.3", optional = true }
3030
staticfile = { version = "0.2", optional = true }
31-
websocket = { version = "0.16.1", optional = true}
31+
ws = { version = "0.4.6", optional = true}
3232

3333

3434
# Tests
@@ -42,7 +42,7 @@ debug = []
4242
output = []
4343
regenerate-css = []
4444
watch = ["notify", "time", "crossbeam"]
45-
serve = ["iron", "staticfile", "websocket"]
45+
serve = ["iron", "staticfile", "ws"]
4646

4747
[[bin]]
4848
doc = false

src/bin/livereload.rs

Lines changed: 0 additions & 210 deletions
This file was deleted.

src/bin/mdbook.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ extern crate time;
1515
extern crate iron;
1616
#[cfg(feature = "serve")]
1717
extern crate staticfile;
18-
1918
#[cfg(feature = "serve")]
20-
mod livereload;
19+
extern crate ws;
2120

2221
use std::env;
2322
use std::error::Error;
@@ -32,10 +31,6 @@ use notify::Watcher;
3231
#[cfg(feature = "watch")]
3332
use std::sync::mpsc::channel;
3433

35-
// Uses for the Serve feature
36-
#[cfg(feature = "serve")]
37-
use livereload::LiveReload;
38-
3934

4035
use mdbook::MDBook;
4136

@@ -187,16 +182,21 @@ fn watch(args: &ArgMatches) -> Result<(), Box<Error>> {
187182
// Watch command implementation
188183
#[cfg(feature = "serve")]
189184
fn 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: {:?}\nBuilding 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!("\nListening for changes...\n");
295+
285296
loop {
286297
match rx.recv() {
287298
Ok(event) => {

0 commit comments

Comments
 (0)