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::borrow::ToOwned;
|
||||
use util::str::DOMString;
|
||||
use util::task::spawn_named;
|
||||
|
||||
use hyper::header::Host;
|
||||
use websocket::Message;
|
||||
|
@ -101,6 +102,9 @@ impl WebSocket {
|
|||
let parsed_url = try!(Url::parse(&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.
|
||||
let protocols = protocols.as_slice();
|
||||
|
||||
|
@ -121,54 +125,41 @@ impl WebSocket {
|
|||
}
|
||||
}
|
||||
|
||||
/*TODO: This constructor is only a prototype, it does not accomplish the specs
|
||||
defined here:
|
||||
http://html.spec.whatwg.org
|
||||
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
|
||||
*/
|
||||
// Step 6: Origin.
|
||||
|
||||
// Step 7.
|
||||
let ws = reflect_dom_object(box WebSocket::new_inherited(global, parsed_url),
|
||||
global,
|
||||
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 sender = global.script_chan();
|
||||
spawn_named(format!("WebSocket connection to {}", ws.Url()), move || {
|
||||
// Step 8: Protocols.
|
||||
|
||||
// Step 9.
|
||||
let channel = establish_a_websocket_connection(url, origin);
|
||||
let (temp_sender, _temp_receiver) = match channel {
|
||||
Ok(channel) => channel,
|
||||
Err(e) => {
|
||||
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);
|
||||
sender.send(ScriptMsg::RunnableMsg(task)).unwrap();
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
//Create everything necessary for starting the open asynchronous task, then begin the task.
|
||||
let global_root = ws.r().global.root();
|
||||
let addr: Trusted<WebSocket> =
|
||||
Trusted::new(global_root.r().get_cx(), ws.r(), global_root.r().script_chan().clone());
|
||||
let open_task = box ConnectionEstablishedTask {
|
||||
addr: addr,
|
||||
addr: address,
|
||||
sender: temp_sender,
|
||||
};
|
||||
global_root.r().script_chan().send(ScriptMsg::RunnableMsg(open_task)).unwrap();
|
||||
//TODO: Spawn thread here for receive loop
|
||||
/*TODO: Add receive loop here and make new thread run this
|
||||
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:
|
||||
https://github.com/cyderize/rust-websocket/blob/master/examples/client.rs#L64
|
||||
TODO: The receive loop also needs to dispatch an asynchronous event as stated here:
|
||||
https://github.com/cyderize/rust-websocket/blob/master/examples/client.rs#L64
|
||||
TODO: When the receive loop receives a close message from the server,
|
||||
it confirms the websocket is now closed. This requires the close event
|
||||
to be fired (dispatch_close fires the close event - see implementation below)
|
||||
*/
|
||||
sender.send(ScriptMsg::RunnableMsg(open_task)).unwrap();
|
||||
});
|
||||
|
||||
// Step 7.
|
||||
Ok(ws)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue