Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Wasm Sockets
Manage
Activity
Members
Labels
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Microhacks
Wasm Sockets
Commits
eb7c5cd0
Unverified
Commit
eb7c5cd0
authored
4 years ago
by
scratchyone
Browse files
Options
Downloads
Patches
Plain Diff
Add panic hook to examples
parent
4565acaf
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
examples/event.rs
+2
-2
2 additions, 2 deletions
examples/event.rs
examples/polling.rs
+14
-4
14 additions, 4 deletions
examples/polling.rs
src/lib.rs
+10
-1
10 additions, 1 deletion
src/lib.rs
with
26 additions
and
7 deletions
examples/event.rs
+
2
−
2
View file @
eb7c5cd0
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
));
...
...
This diff is collapsed.
Click to expand it.
examples/polling.rs
+
14
−
4
View file @
eb7c5cd0
...
@@ -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
;
...
...
This diff is collapsed.
Click to expand it.
src/lib.rs
+
10
−
1
View file @
eb7c5cd0
...
@@ -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()>);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment