From eb7c5cd044c5bd7f235c2c0303aeafaea5c0aef1 Mon Sep 17 00:00:00 2001
From: scratchyone <scratchywon@gmail.com>
Date: Wed, 23 Dec 2020 22:59:06 -0500
Subject: [PATCH] Add panic hook to examples

---
 examples/event.rs   |  4 ++--
 examples/polling.rs | 18 ++++++++++++++----
 src/lib.rs          | 11 ++++++++++-
 3 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/examples/event.rs b/examples/event.rs
index 0fab3f3..18d4bd8 100644
--- a/examples/event.rs
+++ b/examples/event.rs
@@ -1,9 +1,9 @@
+use console_error_panic_hook;
 use console_log;
 use log::{error, info, Level};
+use std::panic;
 use wasm_bindgen::JsValue;
 use wasm_sockets;
-use console_error_panic_hook;
-use std::panic;
 
 fn main() -> Result<(), JsValue> {
     panic::set_hook(Box::new(console_error_panic_hook::hook));
diff --git a/examples/polling.rs b/examples/polling.rs
index cd40040..e7eb6a8 100644
--- a/examples/polling.rs
+++ b/examples/polling.rs
@@ -4,27 +4,37 @@ use std::cell::RefCell;
 use std::panic;
 use std::rc::Rc;
 use wasm_bindgen::prelude::*;
-use wasm_sockets::{self};
+use wasm_sockets::{self, ConnectionStatus};
 
 fn main() -> Result<(), JsValue> {
     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");
     info!("Creating connection");
 
     // 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(
         "wss://echo.websocket.org",
     )?));
 
     let f = Closure::wrap(Box::new(move || {
-        info!("{:#?}", client.borrow_mut().receive());
-        info!("{:#?}", client.borrow().status());
+        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
+        info!("New messages: {:#?}", client.borrow_mut().receive());
     }) as Box<dyn FnMut()>);
-    setInterval(&f, 100); // Create non-blocking loop
+
+    // Start non-blocking game loop
+    setInterval(&f, 100);
     f.forget();
 
     Ok(())
 }
+// Bind setInterval to make a basic game loop
 #[wasm_bindgen]
 extern "C" {
     fn setInterval(closure: &Closure<dyn FnMut()>, time: u32) -> i32;
diff --git a/src/lib.rs b/src/lib.rs
index f40300a..e20ff50 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -5,8 +5,11 @@
 //! use wasm_bindgen::JsValue;
 //! use wasm_sockets;
 //! use console_log;
+//! use console_error_panic_hook;
+//! use std::panic;
 //!
 //! fn main() -> Result<(), JsValue> {
+//!     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");
@@ -39,14 +42,16 @@
 //! 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.
 //! ```
+//! use console_error_panic_hook;
 //! use log::{info, Level};
 //! use std::cell::RefCell;
 //! use std::panic;
 //! use std::rc::Rc;
 //! use wasm_bindgen::prelude::*;
-//! use wasm_sockets::{self};
+//! use wasm_sockets::{self, ConnectionStatus};
 //!
 //! fn main() -> Result<(), JsValue> {
+//!     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");
@@ -59,6 +64,10 @@
 //!     )?));
 //!
 //!     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
 //!         info!("New messages: {:#?}", client.borrow_mut().receive());
 //!     }) as Box<dyn FnMut()>);
-- 
GitLab