mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Spawn a thread for WebSocket messages.
This commit is contained in:
parent
91849cb603
commit
32ddd356b8
1 changed files with 34 additions and 43 deletions
|
@ -24,6 +24,7 @@ use script_task::ScriptMsg;
|
||||||
use std::cell::{Cell, RefCell};
|
use std::cell::{Cell, RefCell};
|
||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
use util::str::DOMString;
|
use util::str::DOMString;
|
||||||
|
use util::task::spawn_named;
|
||||||
|
|
||||||
use hyper::header::Host;
|
use hyper::header::Host;
|
||||||
use websocket::Message;
|
use websocket::Message;
|
||||||
|
@ -101,6 +102,9 @@ impl WebSocket {
|
||||||
let parsed_url = try!(Url::parse(&url).map_err(|_| Error::Syntax));
|
let parsed_url = try!(Url::parse(&url).map_err(|_| Error::Syntax));
|
||||||
let url = try!(parse_url(&parsed_url).map_err(|_| Error::Syntax));
|
let url = try!(parse_url(&parsed_url).map_err(|_| Error::Syntax));
|
||||||
|
|
||||||
|
// Step 2: Disallow https -> ws connections.
|
||||||
|
// Step 3: Potentially block access to some ports.
|
||||||
|
|
||||||
// Step 4.
|
// Step 4.
|
||||||
let protocols = protocols.as_slice();
|
let protocols = protocols.as_slice();
|
||||||
|
|
||||||
|
@ -121,54 +125,41 @@ impl WebSocket {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*TODO: This constructor is only a prototype, it does not accomplish the specs
|
// Step 6: Origin.
|
||||||
defined here:
|
|
||||||
http://html.spec.whatwg.org
|
// Step 7.
|
||||||
The remaining 8 items must be satisfied.
|
|
||||||
TODO: This constructor should be responsible for spawning a thread for the
|
|
||||||
receive loop after ws.r().Open() - See comment
|
|
||||||
*/
|
|
||||||
let ws = reflect_dom_object(box WebSocket::new_inherited(global, parsed_url),
|
let ws = reflect_dom_object(box WebSocket::new_inherited(global, parsed_url),
|
||||||
global,
|
global,
|
||||||
WebSocketBinding::Wrap);
|
WebSocketBinding::Wrap);
|
||||||
|
let address = Trusted::new(global.get_cx(), ws.r(), global.script_chan());
|
||||||
|
|
||||||
let channel = establish_a_websocket_connection(url, global.get_url().serialize());
|
let origin = global.get_url().serialize();
|
||||||
let (temp_sender, _temp_receiver) = match channel {
|
let sender = global.script_chan();
|
||||||
Ok(channel) => channel,
|
spawn_named(format!("WebSocket connection to {}", ws.Url()), move || {
|
||||||
Err(e) => {
|
// Step 8: Protocols.
|
||||||
debug!("Failed to establish a WebSocket connection: {:?}", e);
|
|
||||||
let global_root = ws.r().global.root();
|
|
||||||
let address = Trusted::new(global_root.r().get_cx(), ws.r(), global_root.r().script_chan().clone());
|
|
||||||
let task = box CloseTask {
|
|
||||||
addr: address,
|
|
||||||
};
|
|
||||||
global_root.r().script_chan().send(ScriptMsg::RunnableMsg(task)).unwrap();
|
|
||||||
return Ok(ws);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
//Create everything necessary for starting the open asynchronous task, then begin the task.
|
// Step 9.
|
||||||
let global_root = ws.r().global.root();
|
let channel = establish_a_websocket_connection(url, origin);
|
||||||
let addr: Trusted<WebSocket> =
|
let (temp_sender, _temp_receiver) = match channel {
|
||||||
Trusted::new(global_root.r().get_cx(), ws.r(), global_root.r().script_chan().clone());
|
Ok(channel) => channel,
|
||||||
let open_task = box ConnectionEstablishedTask {
|
Err(e) => {
|
||||||
addr: addr,
|
debug!("Failed to establish a WebSocket connection: {:?}", e);
|
||||||
sender: temp_sender,
|
let task = box CloseTask {
|
||||||
};
|
addr: address,
|
||||||
global_root.r().script_chan().send(ScriptMsg::RunnableMsg(open_task)).unwrap();
|
};
|
||||||
//TODO: Spawn thread here for receive loop
|
sender.send(ScriptMsg::RunnableMsg(task)).unwrap();
|
||||||
/*TODO: Add receive loop here and make new thread run this
|
return;
|
||||||
Receive is an infinite loop "similiar" the one shown here:
|
}
|
||||||
https://github.com/cyderize/rust-websocket/blob/master/examples/client.rs#L64
|
};
|
||||||
TODO: The receive loop however does need to follow the spec. These are outlined here
|
|
||||||
under "WebSocket message has been received" items 1-5:
|
let open_task = box ConnectionEstablishedTask {
|
||||||
https://github.com/cyderize/rust-websocket/blob/master/examples/client.rs#L64
|
addr: address,
|
||||||
TODO: The receive loop also needs to dispatch an asynchronous event as stated here:
|
sender: temp_sender,
|
||||||
https://github.com/cyderize/rust-websocket/blob/master/examples/client.rs#L64
|
};
|
||||||
TODO: When the receive loop receives a close message from the server,
|
sender.send(ScriptMsg::RunnableMsg(open_task)).unwrap();
|
||||||
it confirms the websocket is now closed. This requires the close event
|
});
|
||||||
to be fired (dispatch_close fires the close event - see implementation below)
|
|
||||||
*/
|
// Step 7.
|
||||||
Ok(ws)
|
Ok(ws)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue