Skip to content

Commit

Permalink
Add HTML files to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
scratchyone committed Dec 24, 2020
1 parent 4bae14d commit 36d086e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
28 changes: 28 additions & 0 deletions examples/event.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<html>
<head>
<meta charset="UTF-8" />
<style>
body {
background: linear-gradient(
135deg,
white 0%,
white 49%,
black 49%,
black 51%,
white 51%,
white 100%
);
background-repeat: repeat;
background-size: 20px 20px;
margin: 0px;
}
canvas {
background-color: white;
}
</style>
</head>
<script type="module">
import init from '../target/event.js';
init();
</script>
</html>
4 changes: 3 additions & 1 deletion examples/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ fn main() -> Result<(), JsValue> {
c.send_string("test...").unwrap();
c.send_binary(vec![20]).unwrap();
})));

client.set_on_close(Some(Box::new(|| {
info!("Closed");
})));
client.set_on_message(Some(Box::new(
|c: &wasm_sockets::EventClient, e: wasm_sockets::Message| {
info!("New Message: {:#?}", e);
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions examples/polling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn main() -> Result<(), JsValue> {

let f = Closure::wrap(Box::new(move || {
info!("{:#?}", client.borrow_mut().receive());
info!("{:#?}", client.borrow().status());
}) as Box<dyn FnMut()>);
setInterval(&f, 100); // Create non-blocking loop
f.forget();
Expand Down
37 changes: 37 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub enum ConnectionStatus {
Connected,
/// Disconnected from a server due to an error
Error(ErrorEvent),
/// Disconnected from a server without an error
Disconnected,
}

/// Message is a representation of a websocket message that can be sent or recieved
Expand Down Expand Up @@ -59,6 +61,12 @@ impl PollingClient {
*status_ref.borrow_mut() = ConnectionStatus::Error(e);
})));

let status_ref = status.clone();

client.set_on_close(Some(Box::new(move || {
*status_ref.borrow_mut() = ConnectionStatus::Disconnected;
})));

client.set_on_message(Some(Box::new(move |c: &EventClient, m: Message| {
data_ref.borrow_mut().push(m);
})));
Expand Down Expand Up @@ -114,6 +122,8 @@ pub struct EventClient {
pub on_connection: Rc<RefCell<Option<Box<dyn Fn(&EventClient, JsValue) -> ()>>>>,
/// The function bound to the on_message event
pub on_message: Rc<RefCell<Option<Box<dyn Fn(&EventClient, Message) -> ()>>>>,
/// The function bound to the on_close event
pub on_close: Rc<RefCell<Option<Box<dyn Fn() -> ()>>>>,
}
impl EventClient {
/// Create a new EventClient and connect to a WebSocket URL
Expand Down Expand Up @@ -144,6 +154,19 @@ impl EventClient {
ws.set_onerror(Some(onerror_callback.as_ref().unchecked_ref()));
onerror_callback.forget();

let on_close: Rc<RefCell<Option<Box<dyn Fn() -> ()>>>> = Rc::new(RefCell::new(None));
let on_close_ref = on_close.clone();
let ref_status = status.clone();

let onclose_callback = Closure::wrap(Box::new(move || {
*ref_status.borrow_mut() = ConnectionStatus::Disconnected;
if let Some(f) = &*on_close_ref.borrow() {
f.as_ref()();
}
}) as Box<dyn FnMut()>);
ws.set_onclose(Some(onclose_callback.as_ref().unchecked_ref()));
onclose_callback.forget();

let on_connection: Rc<RefCell<Option<Box<dyn Fn(&EventClient, JsValue) -> ()>>>> =
Rc::new(RefCell::new(None));
let on_connection_ref = on_connection.clone();
Expand All @@ -164,6 +187,7 @@ impl EventClient {
on_connection: on_connection.clone(),
status: status.clone(),
on_message: on_message.clone(),
on_close: on_close.clone(),
}));
let client_ref = client.clone();

Expand Down Expand Up @@ -231,6 +255,7 @@ impl EventClient {
on_error,
on_connection,
on_message,
on_close,
status: status,
})
}
Expand Down Expand Up @@ -272,6 +297,18 @@ impl EventClient {
pub fn set_on_message(&mut self, f: Option<Box<dyn Fn(&EventClient, Message) -> ()>>) {
*self.on_message.borrow_mut() = f;
}
/// Set an on_close event handler.
/// This handler will be run when the client disconnects from a server without an error.
/// This will overwrite the previous handler.
/// You can set [None](std::option) to disable the on_close handler.
/// ```
/// client.set_on_close(Some(Box::new(|| {
/// info!("Closed");
/// })));
/// ```
pub fn set_on_close(&mut self, f: Option<Box<dyn Fn() -> ()>>) {
*self.on_close.borrow_mut() = f;
}

/// Send a text message to the server
/// ```
Expand Down

0 comments on commit 36d086e

Please sign in to comment.