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
90a3f1a9
Unverified
Commit
90a3f1a9
authored
4 years ago
by
scratchyone
Browse files
Options
Downloads
Patches
Plain Diff
Improve API
parent
9c1033f2
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/basic.rs
+8
-10
8 additions, 10 deletions
examples/basic.rs
src/lib.rs
+11
-18
11 additions, 18 deletions
src/lib.rs
with
19 additions
and
28 deletions
examples/basic.rs
+
8
−
10
View file @
90a3f1a9
...
@@ -15,18 +15,16 @@ fn main() -> Result<(), JsValue> {
...
@@ -15,18 +15,16 @@ fn main() -> Result<(), JsValue> {
client
.set_on_error
(
Some
(
Box
::
new
(|
e
|
{
client
.set_on_error
(
Some
(
Box
::
new
(|
e
|
{
error!
(
"{:#?}"
,
e
);
error!
(
"{:#?}"
,
e
);
})));
})));
client
.set_on_connection
(
Some
(
Box
::
new
(
client
.set_on_connection
(
Some
(
Box
::
new
(|
c
:
&
wasm_sockets
::
EventClient
,
e
|
{
|
c
:
Rc
<
RefCell
<
wasm_sockets
::
EventClient
>>
,
e
|
{
info!
(
"Connected: {:#?}"
,
e
);
info!
(
"Connected: {:#?}"
,
e
);
info!
(
"{:#?}"
,
c
.status
);
info!
(
"{:#?}"
,
&
c
.borrow_mut
()
.status
);
info!
(
"Sending message..."
);
info!
(
"Sending message..."
);
c
.send_string
(
"test..."
)
.unwrap
();
c
.borrow
()
.send_string
(
"test..."
)
.unwrap
();
c
.send_binary
(
vec!
[
20
])
.unwrap
();
c
.borrow
()
.send_binary
(
vec!
[
20
])
.unwrap
();
})));
},
)));
client
.set_on_message
(
Some
(
Box
::
new
(
client
.set_on_message
(
Some
(
Box
::
new
(
|
c
:
Rc
<
RefCell
<
wasm_sockets
::
EventClient
>>
,
e
:
wasm_sockets
::
Message
|
{
|
c
:
&
wasm_sockets
::
EventClient
,
e
:
wasm_sockets
::
Message
|
{
info!
(
"New Message: {:#?}"
,
e
);
info!
(
"New Message: {:#?}"
,
e
);
},
},
)));
)));
...
...
This diff is collapsed.
Click to expand it.
src/lib.rs
+
11
−
18
View file @
90a3f1a9
...
@@ -128,8 +128,8 @@ pub struct EventClient {
...
@@ -128,8 +128,8 @@ pub struct EventClient {
pub
connection
:
Rc
<
RefCell
<
web_sys
::
WebSocket
>>
,
pub
connection
:
Rc
<
RefCell
<
web_sys
::
WebSocket
>>
,
pub
status
:
Rc
<
RefCell
<
ConnectionStatus
>>
,
pub
status
:
Rc
<
RefCell
<
ConnectionStatus
>>
,
pub
on_error
:
Rc
<
RefCell
<
Option
<
Box
<
dyn
Fn
(
ErrorEvent
)
->
()
>>>>
,
pub
on_error
:
Rc
<
RefCell
<
Option
<
Box
<
dyn
Fn
(
ErrorEvent
)
->
()
>>>>
,
pub
on_connection
:
Rc
<
RefCell
<
Option
<
Box
<
dyn
Fn
(
Rc
<
RefCell
<
EventClient
>>
,
JsValue
)
->
()
>>>>
,
pub
on_connection
:
Rc
<
RefCell
<
Option
<
Box
<
dyn
Fn
(
&
EventClient
,
JsValue
)
->
()
>>>>
,
pub
on_message
:
Rc
<
RefCell
<
Option
<
Box
<
dyn
Fn
(
Rc
<
RefCell
<
EventClient
>>
,
Message
)
->
()
>>>>
,
pub
on_message
:
Rc
<
RefCell
<
Option
<
Box
<
dyn
Fn
(
&
EventClient
,
Message
)
->
()
>>>>
,
}
}
impl
EventClient
{
impl
EventClient
{
pub
fn
new
(
url
:
&
str
)
->
Result
<
Self
,
JsValue
>
{
pub
fn
new
(
url
:
&
str
)
->
Result
<
Self
,
JsValue
>
{
...
@@ -154,12 +154,11 @@ impl EventClient {
...
@@ -154,12 +154,11 @@ impl EventClient {
ws
.set_onerror
(
Some
(
onerror_callback
.as_ref
()
.unchecked_ref
()));
ws
.set_onerror
(
Some
(
onerror_callback
.as_ref
()
.unchecked_ref
()));
onerror_callback
.forget
();
onerror_callback
.forget
();
let
on_connection
:
Rc
<
let
on_connection
:
Rc
<
RefCell
<
Option
<
Box
<
dyn
Fn
(
&
EventClient
,
JsValue
)
->
()
>>>>
=
RefCell
<
Option
<
Box
<
dyn
Fn
(
Rc
<
RefCell
<
EventClient
>>
,
JsValue
)
->
()
>>>
,
Rc
::
new
(
RefCell
::
new
(
None
));
>
=
Rc
::
new
(
RefCell
::
new
(
None
));
let
on_connection_ref
=
on_connection
.clone
();
let
on_connection_ref
=
on_connection
.clone
();
let
on_message
:
Rc
<
RefCell
<
Option
<
Box
<
dyn
Fn
(
Rc
<
RefCell
<
EventClient
>>
,
Message
)
->
()
>>>>
=
let
on_message
:
Rc
<
RefCell
<
Option
<
Box
<
dyn
Fn
(
&
EventClient
,
Message
)
->
()
>>>>
=
Rc
::
new
(
RefCell
::
new
(
None
));
Rc
::
new
(
RefCell
::
new
(
None
));
let
on_message_ref
=
on_message
.clone
();
let
on_message_ref
=
on_message
.clone
();
...
@@ -181,7 +180,7 @@ impl EventClient {
...
@@ -181,7 +180,7 @@ impl EventClient {
let
onopen_callback
=
Closure
::
wrap
(
Box
::
new
(
move
|
v
|
{
let
onopen_callback
=
Closure
::
wrap
(
Box
::
new
(
move
|
v
|
{
*
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
(),
v
);
f
.as_ref
()(
&*
client_ref
.clone
()
.borrow
()
,
v
);
}
}
})
as
Box
<
dyn
FnMut
(
JsValue
)
>
);
})
as
Box
<
dyn
FnMut
(
JsValue
)
>
);
connection
connection
...
@@ -200,7 +199,7 @@ impl EventClient {
...
@@ -200,7 +199,7 @@ impl EventClient {
// Convert arraybuffer to vec
// Convert arraybuffer to vec
let
array
=
js_sys
::
Uint8Array
::
new
(
&
abuf
)
.to_vec
();
let
array
=
js_sys
::
Uint8Array
::
new
(
&
abuf
)
.to_vec
();
if
let
Some
(
f
)
=
&*
on_message_ref
.borrow
()
{
if
let
Some
(
f
)
=
&*
on_message_ref
.borrow
()
{
f
.as_ref
()(
client_ref
.clone
(),
Message
::
Binary
(
array
));
f
.as_ref
()(
&*
client_ref
.clone
()
.borrow
()
,
Message
::
Binary
(
array
));
}
}
}
else
if
let
Ok
(
blob
)
=
e
.data
()
.dyn_into
::
<
web_sys
::
Blob
>
()
{
}
else
if
let
Ok
(
blob
)
=
e
.data
()
.dyn_into
::
<
web_sys
::
Blob
>
()
{
// Received blob data
// Received blob data
...
@@ -213,7 +212,7 @@ impl EventClient {
...
@@ -213,7 +212,7 @@ impl EventClient {
let
onloadend_cb
=
Closure
::
wrap
(
Box
::
new
(
move
|
_e
:
web_sys
::
ProgressEvent
|
{
let
onloadend_cb
=
Closure
::
wrap
(
Box
::
new
(
move
|
_e
:
web_sys
::
ProgressEvent
|
{
let
array
=
js_sys
::
Uint8Array
::
new
(
&
fr_c
.result
()
.unwrap
())
.to_vec
();
let
array
=
js_sys
::
Uint8Array
::
new
(
&
fr_c
.result
()
.unwrap
())
.to_vec
();
if
let
Some
(
f
)
=
&*
cbref
.borrow
()
{
if
let
Some
(
f
)
=
&*
cbref
.borrow
()
{
f
.as_ref
()(
cbfref
.clone
(),
Message
::
Binary
(
array
));
f
.as_ref
()(
&*
cbfref
.clone
()
.borrow
()
,
Message
::
Binary
(
array
));
}
}
})
})
as
Box
<
dyn
FnMut
(
web_sys
::
ProgressEvent
)
>
);
as
Box
<
dyn
FnMut
(
web_sys
::
ProgressEvent
)
>
);
...
@@ -222,7 +221,7 @@ impl EventClient {
...
@@ -222,7 +221,7 @@ impl EventClient {
onloadend_cb
.forget
();
onloadend_cb
.forget
();
}
else
if
let
Ok
(
txt
)
=
e
.data
()
.dyn_into
::
<
js_sys
::
JsString
>
()
{
}
else
if
let
Ok
(
txt
)
=
e
.data
()
.dyn_into
::
<
js_sys
::
JsString
>
()
{
if
let
Some
(
f
)
=
&*
on_message_ref
.borrow
()
{
if
let
Some
(
f
)
=
&*
on_message_ref
.borrow
()
{
f
.as_ref
()(
client_ref
.clone
(),
Message
::
Text
(
txt
.into
()));
f
.as_ref
()(
&*
client_ref
.clone
()
.borrow
()
,
Message
::
Text
(
txt
.into
()));
}
}
}
else
{
}
else
{
// Got unknown data
// Got unknown data
...
@@ -249,17 +248,11 @@ impl EventClient {
...
@@ -249,17 +248,11 @@ impl EventClient {
pub
fn
set_on_error
(
&
mut
self
,
f
:
Option
<
Box
<
dyn
Fn
(
ErrorEvent
)
->
()
>>
)
{
pub
fn
set_on_error
(
&
mut
self
,
f
:
Option
<
Box
<
dyn
Fn
(
ErrorEvent
)
->
()
>>
)
{
*
self
.on_error
.borrow_mut
()
=
f
;
*
self
.on_error
.borrow_mut
()
=
f
;
}
}
pub
fn
set_on_connection
(
pub
fn
set_on_connection
(
&
mut
self
,
f
:
Option
<
Box
<
dyn
Fn
(
&
EventClient
,
JsValue
)
->
()
>>
)
{
&
mut
self
,
f
:
Option
<
Box
<
dyn
Fn
(
Rc
<
RefCell
<
EventClient
>>
,
JsValue
)
->
()
>>
,
)
{
*
self
.on_connection
.borrow_mut
()
=
f
;
*
self
.on_connection
.borrow_mut
()
=
f
;
}
}
pub
fn
set_on_message
(
pub
fn
set_on_message
(
&
mut
self
,
f
:
Option
<
Box
<
dyn
Fn
(
&
EventClient
,
Message
)
->
()
>>
)
{
&
mut
self
,
f
:
Option
<
Box
<
dyn
Fn
(
Rc
<
RefCell
<
EventClient
>>
,
Message
)
->
()
>>
,
)
{
*
self
.on_message
.borrow_mut
()
=
f
;
*
self
.on_message
.borrow_mut
()
=
f
;
}
}
...
...
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