Skip to content
Snippets Groups Projects
Unverified Commit eb7c5cd0 authored by scratchyone's avatar scratchyone
Browse files

Add panic hook to examples

parent 4565acaf
No related branches found
No related tags found
No related merge requests found
use console_error_panic_hook;
use console_log; use console_log;
use log::{error, info, Level}; use log::{error, info, Level};
use std::panic;
use wasm_bindgen::JsValue; use wasm_bindgen::JsValue;
use wasm_sockets; use wasm_sockets;
use console_error_panic_hook;
use std::panic;
fn main() -> Result<(), JsValue> { fn main() -> Result<(), JsValue> {
panic::set_hook(Box::new(console_error_panic_hook::hook)); panic::set_hook(Box::new(console_error_panic_hook::hook));
......
...@@ -4,27 +4,37 @@ use std::cell::RefCell; ...@@ -4,27 +4,37 @@ use std::cell::RefCell;
use std::panic; use std::panic;
use std::rc::Rc; use std::rc::Rc;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use wasm_sockets::{self}; use wasm_sockets::{self, ConnectionStatus};
fn main() -> Result<(), JsValue> { fn main() -> Result<(), JsValue> {
panic::set_hook(Box::new(console_error_panic_hook::hook)); panic::set_hook(Box::new(console_error_panic_hook::hook));
// console_log and log macros are used instead of println!
// so that messages can be seen in the browser console
console_log::init_with_level(Level::Trace).expect("Failed to enable logging"); console_log::init_with_level(Level::Trace).expect("Failed to enable logging");
info!("Creating connection"); info!("Creating connection");
// Client is wrapped in an Rc<RefCell<>> so it can be used within setInterval // Client is wrapped in an Rc<RefCell<>> so it can be used within setInterval
// This isn't required when being used within a game engine
let client = Rc::new(RefCell::new(wasm_sockets::PollingClient::new( let client = Rc::new(RefCell::new(wasm_sockets::PollingClient::new(
"wss://echo.websocket.org", "wss://echo.websocket.org",
)?)); )?));
let f = Closure::wrap(Box::new(move || { let f = Closure::wrap(Box::new(move || {
info!("{:#?}", client.borrow_mut().receive()); if client.borrow().status() == ConnectionStatus::Connected {
info!("{:#?}", client.borrow().status()); info!("Sending message");
client.borrow().send_string("Hello, World!").unwrap();
}
// receive() gives you all new websocket messages since receive() was last called
info!("New messages: {:#?}", client.borrow_mut().receive());
}) as Box<dyn FnMut()>); }) as Box<dyn FnMut()>);
setInterval(&f, 100); // Create non-blocking loop
// Start non-blocking game loop
setInterval(&f, 100);
f.forget(); f.forget();
Ok(()) Ok(())
} }
// Bind setInterval to make a basic game loop
#[wasm_bindgen] #[wasm_bindgen]
extern "C" { extern "C" {
fn setInterval(closure: &Closure<dyn FnMut()>, time: u32) -> i32; fn setInterval(closure: &Closure<dyn FnMut()>, time: u32) -> i32;
......
...@@ -5,8 +5,11 @@ ...@@ -5,8 +5,11 @@
//! use wasm_bindgen::JsValue; //! use wasm_bindgen::JsValue;
//! use wasm_sockets; //! use wasm_sockets;
//! use console_log; //! use console_log;
//! use console_error_panic_hook;
//! use std::panic;
//! //!
//! fn main() -> Result<(), JsValue> { //! fn main() -> Result<(), JsValue> {
//! panic::set_hook(Box::new(console_error_panic_hook::hook));
//! // console_log and log macros are used instead of println! //! // console_log and log macros are used instead of println!
//! // so that messages can be seen in the browser console //! // so that messages can be seen in the browser console
//! console_log::init_with_level(Level::Trace).expect("Failed to enable logging"); //! console_log::init_with_level(Level::Trace).expect("Failed to enable logging");
...@@ -39,14 +42,16 @@ ...@@ -39,14 +42,16 @@
//! This client is also much simpler than the [`EventClient`]. However, you can access the main [`EventClient`] that it is using //! This client is also much simpler than the [`EventClient`]. However, you can access the main [`EventClient`] that it is using
//! if you want access to lower level control. //! if you want access to lower level control.
//! ``` //! ```
//! use console_error_panic_hook;
//! use log::{info, Level}; //! use log::{info, Level};
//! use std::cell::RefCell; //! use std::cell::RefCell;
//! use std::panic; //! use std::panic;
//! use std::rc::Rc; //! use std::rc::Rc;
//! use wasm_bindgen::prelude::*; //! use wasm_bindgen::prelude::*;
//! use wasm_sockets::{self}; //! use wasm_sockets::{self, ConnectionStatus};
//! //!
//! fn main() -> Result<(), JsValue> { //! fn main() -> Result<(), JsValue> {
//! panic::set_hook(Box::new(console_error_panic_hook::hook));
//! // console_log and log macros are used instead of println! //! // console_log and log macros are used instead of println!
//! // so that messages can be seen in the browser console //! // so that messages can be seen in the browser console
//! console_log::init_with_level(Level::Trace).expect("Failed to enable logging"); //! console_log::init_with_level(Level::Trace).expect("Failed to enable logging");
...@@ -59,6 +64,10 @@ ...@@ -59,6 +64,10 @@
//! )?)); //! )?));
//! //!
//! let f = Closure::wrap(Box::new(move || { //! let f = Closure::wrap(Box::new(move || {
//! if client.borrow().status() == ConnectionStatus::Connected {
//! info!("Sending message");
//! client.borrow().send_string("Hello, World!").unwrap();
//! }
//! // receive() gives you all new websocket messages since receive() was last called //! // receive() gives you all new websocket messages since receive() was last called
//! info!("New messages: {:#?}", client.borrow_mut().receive()); //! info!("New messages: {:#?}", client.borrow_mut().receive());
//! }) as Box<dyn FnMut()>); //! }) as Box<dyn FnMut()>);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment