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
431c4807
Unverified
Commit
431c4807
authored
4 years ago
by
scratchyone
Browse files
Options
Downloads
Patches
Plain Diff
Remove JsValue from on_connection
parent
dca269ad
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/event.rs
+1
-1
1 addition, 1 deletion
examples/event.rs
src/lib.rs
+12
-12
12 additions, 12 deletions
src/lib.rs
with
13 additions
and
13 deletions
examples/event.rs
+
1
−
1
View file @
431c4807
...
@@ -16,7 +16,7 @@ fn main() -> Result<(), JsValue> {
...
@@ -16,7 +16,7 @@ fn main() -> Result<(), JsValue> {
client
.set_on_error
(
Some
(
Box
::
new
(|
error
|
{
client
.set_on_error
(
Some
(
Box
::
new
(|
error
|
{
error!
(
"{:#?}"
,
error
);
error!
(
"{:#?}"
,
error
);
})));
})));
client
.set_on_connection
(
Some
(
Box
::
new
(|
client
:
&
wasm_sockets
::
EventClient
,
e
|
{
client
.set_on_connection
(
Some
(
Box
::
new
(|
client
:
&
wasm_sockets
::
EventClient
|
{
info!
(
"{:#?}"
,
client
.status
);
info!
(
"{:#?}"
,
client
.status
);
info!
(
"Sending message..."
);
info!
(
"Sending message..."
);
client
.send_string
(
"Hello, World!"
)
.unwrap
();
client
.send_string
(
"Hello, World!"
)
.unwrap
();
...
...
This diff is collapsed.
Click to expand it.
src/lib.rs
+
12
−
12
View file @
431c4807
//! This crate offers 2 (wasm-only) websocket clients.
//! This crate offers 2 (wasm-only) websocket clients.
//! The first client offered is the [`EventClient`]. This client is event based and gives you the most control.
//! The first client offered is the [`EventClient`]. This client is event based and gives you the most control.
//! ```
//! ```
//! use console_error_panic_hook;
//! 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_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));
//! panic::set_hook(Box::new(console_error_panic_hook::hook));
...
@@ -19,7 +19,7 @@
...
@@ -19,7 +19,7 @@
//! client.set_on_error(Some(Box::new(|error| {
//! client.set_on_error(Some(Box::new(|error| {
//! error!("{:#?}", error);
//! error!("{:#?}", error);
//! })));
//! })));
//! client.set_on_connection(Some(Box::new(|client: &wasm_sockets::EventClient
, e
| {
//! client.set_on_connection(Some(Box::new(|client: &wasm_sockets::EventClient| {
//! info!("{:#?}", client.status);
//! info!("{:#?}", client.status);
//! info!("Sending message...");
//! info!("Sending message...");
//! client.send_string("Hello, World!").unwrap();
//! client.send_string("Hello, World!").unwrap();
...
@@ -137,7 +137,7 @@ impl PollingClient {
...
@@ -137,7 +137,7 @@ impl PollingClient {
let
status
=
Rc
::
new
(
RefCell
::
new
(
ConnectionStatus
::
Connecting
));
let
status
=
Rc
::
new
(
RefCell
::
new
(
ConnectionStatus
::
Connecting
));
let
status_ref
=
status
.clone
();
let
status_ref
=
status
.clone
();
client
.set_on_connection
(
Some
(
Box
::
new
(
move
|
_client
,
_e
|
{
client
.set_on_connection
(
Some
(
Box
::
new
(
move
|
_client
|
{
*
status_ref
.borrow_mut
()
=
ConnectionStatus
::
Connected
;
*
status_ref
.borrow_mut
()
=
ConnectionStatus
::
Connected
;
})));
})));
...
@@ -205,7 +205,7 @@ pub struct EventClient {
...
@@ -205,7 +205,7 @@ pub struct EventClient {
/// The function bound to the on_error event
/// The function bound to the on_error event
pub
on_error
:
Rc
<
RefCell
<
Option
<
Box
<
dyn
Fn
(
ErrorEvent
)
->
()
>>>>
,
pub
on_error
:
Rc
<
RefCell
<
Option
<
Box
<
dyn
Fn
(
ErrorEvent
)
->
()
>>>>
,
/// The function bound to the on_connection event
/// The function bound to the on_connection event
pub
on_connection
:
Rc
<
RefCell
<
Option
<
Box
<
dyn
Fn
(
&
EventClient
,
JsValue
)
->
()
>>>>
,
pub
on_connection
:
Rc
<
RefCell
<
Option
<
Box
<
dyn
Fn
(
&
EventClient
)
->
()
>>>>
,
/// The function bound to the on_message event
/// The function bound to the on_message event
pub
on_message
:
Rc
<
RefCell
<
Option
<
Box
<
dyn
Fn
(
&
EventClient
,
Message
)
->
()
>>>>
,
pub
on_message
:
Rc
<
RefCell
<
Option
<
Box
<
dyn
Fn
(
&
EventClient
,
Message
)
->
()
>>>>
,
/// The function bound to the on_close event
/// The function bound to the on_close event
...
@@ -253,7 +253,7 @@ impl EventClient {
...
@@ -253,7 +253,7 @@ impl EventClient {
ws
.set_onclose
(
Some
(
onclose_callback
.as_ref
()
.unchecked_ref
()));
ws
.set_onclose
(
Some
(
onclose_callback
.as_ref
()
.unchecked_ref
()));
onclose_callback
.forget
();
onclose_callback
.forget
();
let
on_connection
:
Rc
<
RefCell
<
Option
<
Box
<
dyn
Fn
(
&
EventClient
,
JsValue
)
->
()
>>>>
=
let
on_connection
:
Rc
<
RefCell
<
Option
<
Box
<
dyn
Fn
(
&
EventClient
)
->
()
>>>>
=
Rc
::
new
(
RefCell
::
new
(
None
));
Rc
::
new
(
RefCell
::
new
(
None
));
let
on_connection_ref
=
on_connection
.clone
();
let
on_connection_ref
=
on_connection
.clone
();
...
@@ -276,10 +276,10 @@ impl EventClient {
...
@@ -276,10 +276,10 @@ impl EventClient {
}));
}));
let
client_ref
=
client
.clone
();
let
client_ref
=
client
.clone
();
let
onopen_callback
=
Closure
::
wrap
(
Box
::
new
(
move
|
v
|
{
let
onopen_callback
=
Closure
::
wrap
(
Box
::
new
(
move
|
_
|
{
*
ref_status
.borrow_mut
()
=
ConnectionStatus
::
Connected
;
*
ref_status
.borrow_mut
()
=
ConnectionStatus
::
Connected
;
if
let
Some
(
f
)
=
&*
on_connection_ref
.borrow
()
{
if
let
Some
(
f
)
=
&*
on_connection_ref
.borrow
()
{
f
.as_ref
()(
&*
client_ref
.clone
()
.borrow
()
,
v
);
f
.as_ref
()(
&*
client_ref
.clone
()
.borrow
());
}
}
})
as
Box
<
dyn
FnMut
(
JsValue
)
>
);
})
as
Box
<
dyn
FnMut
(
JsValue
)
>
);
connection
connection
...
@@ -360,11 +360,11 @@ impl EventClient {
...
@@ -360,11 +360,11 @@ impl EventClient {
/// This will overwrite the previous handler.
/// This will overwrite the previous handler.
/// You can set [None](std::option) to disable the on_connection handler.
/// You can set [None](std::option) to disable the on_connection handler.
/// ```
/// ```
/// client.set_on_connection(Some(Box::new(|c
, v
| {
/// client.set_on_connection(Some(Box::new(|c
lient
| {
/// info!("Connected
: {:#?}", e
);
/// info!("Connected
"
);
/// })));
/// })));
/// ```
/// ```
pub
fn
set_on_connection
(
&
mut
self
,
f
:
Option
<
Box
<
dyn
Fn
(
&
EventClient
,
JsValue
)
->
()
>>
)
{
pub
fn
set_on_connection
(
&
mut
self
,
f
:
Option
<
Box
<
dyn
Fn
(
&
EventClient
)
->
()
>>
)
{
*
self
.on_connection
.borrow_mut
()
=
f
;
*
self
.on_connection
.borrow_mut
()
=
f
;
}
}
/// Set an on_message event handler.
/// Set an on_message event handler.
...
...
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