WebSocket: Implement WebSocket#binaryType

This commit is contained in:
Tom Jakubowski 2015-08-13 00:25:45 -07:00
parent f77973c903
commit e92f4629db
6 changed files with 34 additions and 33 deletions

View file

@ -27,7 +27,7 @@ interface WebSocket : EventTarget {
//messaging
attribute EventHandler onmessage;
//attribute BinaryType binaryType;
attribute BinaryType binaryType;
[Throws] void send(optional USVString data);
//void send(Blob data);
//void send(ArrayBuffer data);

View file

@ -4,7 +4,7 @@
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::WebSocketBinding;
use dom::bindings::codegen::Bindings::WebSocketBinding::WebSocketMethods;
use dom::bindings::codegen::Bindings::WebSocketBinding::{BinaryType, WebSocketMethods};
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
use dom::bindings::codegen::InheritTypes::EventTargetCast;
use dom::bindings::codegen::InheritTypes::EventCast;
@ -30,8 +30,10 @@ use util::str::DOMString;
use util::task::spawn_named;
use js::jsapi::{RootedValue, JSAutoRequest, JSAutoCompartment};
use js::jsapi::{JS_NewArrayBuffer, JS_GetArrayBufferData};
use js::jsval::UndefinedValue;
use hyper::header::Host;
use libc::{uint8_t, uint32_t};
use websocket::Message;
use websocket::ws::sender::Sender as Sender_Object;
use websocket::client::sender::Sender;
@ -46,6 +48,7 @@ use websocket::ws::util::url::parse_url;
use std::borrow::ToOwned;
use std::cell::{Cell, RefCell};
use std::ptr;
use std::sync::{Arc, Mutex};
#[derive(JSTraceable, PartialEq, Copy, Clone, Debug)]
@ -76,6 +79,7 @@ pub struct WebSocket {
code: Cell<u16>, //Closing code
reason: DOMRefCell<DOMString>, //Closing reason
data: DOMRefCell<DOMString>, //Data from send - TODO: Remove after buffer is added.
binary_type: Cell<BinaryType>,
}
/// *Establish a WebSocket Connection* as defined in RFC 6455.
@ -115,6 +119,7 @@ impl WebSocket {
code: Cell::new(0),
reason: DOMRefCell::new("".to_owned()),
data: DOMRefCell::new("".to_owned()),
binary_type: Cell::new(BinaryType::Blob),
}
}
@ -236,6 +241,16 @@ impl<'a> WebSocketMethods for &'a WebSocket {
self.ready_state.get() as u16
}
// https://html.spec.whatwg.org/multipage/#dom-websocket-binarytype
fn BinaryType(self) -> BinaryType {
self.binary_type.get()
}
// https://html.spec.whatwg.org/multipage/#dom-websocket-binarytype
fn SetBinaryType(self, btype: BinaryType) {
self.binary_type.set(btype)
}
// https://html.spec.whatwg.org/multipage/#dom-websocket-send
fn Send(self, data: Option<USVString>) -> Fallible<()> {
match self.ready_state.get() {
@ -394,6 +409,7 @@ struct MessageReceivedTask {
}
impl Runnable for MessageReceivedTask {
#[allow(unsafe_code)]
fn handler(self: Box<Self>) {
let ws = self.address.root();
debug!("MessageReceivedTask::handler({:p}): readyState={:?}", &*ws,
@ -413,8 +429,22 @@ impl Runnable for MessageReceivedTask {
match self.message {
MessageData::Text(text) => text.to_jsval(cx, message.handle_mut()),
MessageData::Binary(data) => {
let blob = Blob::new(global.r(), Some(data), "");
blob.to_jsval(cx, message.handle_mut());
match ws.binary_type.get() {
BinaryType::Blob => {
let blob = Blob::new(global.r(), Some(data), "");
blob.to_jsval(cx, message.handle_mut());
}
BinaryType::Arraybuffer => {
unsafe {
let len = data.len() as uint32_t;
let buf = JS_NewArrayBuffer(cx, len);
let buf_data: *mut uint8_t = JS_GetArrayBufferData(buf, ptr::null());
ptr::copy_nonoverlapping(data.as_ptr(), buf_data, len as usize);
buf.to_jsval(cx, message.handle_mut());
}
}
}
},
}