aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/websocket.rs
diff options
context:
space:
mode:
authorMs2ger <ms2ger@gmail.com>2015-07-13 14:53:21 +0200
committerMs2ger <ms2ger@gmail.com>2015-07-22 20:29:32 +0200
commit32ddd356b8eca20bca0e73c001bce01a34b62bca (patch)
tree95e53874221609d174c394d3aa51ba914b4c3f67 /components/script/dom/websocket.rs
parent91849cb603a43ecdc403d059788ae79fb28e1f4a (diff)
downloadservo-32ddd356b8eca20bca0e73c001bce01a34b62bca.tar.gz
servo-32ddd356b8eca20bca0e73c001bce01a34b62bca.zip
Spawn a thread for WebSocket messages.
Diffstat (limited to 'components/script/dom/websocket.rs')
-rw-r--r--components/script/dom/websocket.rs79
1 files changed, 35 insertions, 44 deletions
diff --git a/components/script/dom/websocket.rs b/components/script/dom/websocket.rs
index 2e415e5d73d..2ffe520827d 100644
--- a/components/script/dom/websocket.rs
+++ b/components/script/dom/websocket.rs
@@ -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 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 task = box CloseTask {
+ addr: address,
+ };
+ sender.send(ScriptMsg::RunnableMsg(task)).unwrap();
+ return;
+ }
+ };
- let channel = establish_a_websocket_connection(url, global.get_url().serialize());
- 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);
- }
- };
-
- //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,
- 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)
- */
+ let open_task = box ConnectionEstablishedTask {
+ addr: address,
+ sender: temp_sender,
+ };
+ sender.send(ScriptMsg::RunnableMsg(open_task)).unwrap();
+ });
+
+ // Step 7.
Ok(ws)
}