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
11243358
Unverified
Commit
11243358
authored
4 years ago
by
scratchyone
Browse files
Options
Downloads
Patches
Plain Diff
Remove unused example
parent
0624beca
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/lib.rs
+0
-72
0 additions, 72 deletions
src/lib.rs
with
0 additions
and
72 deletions
src/lib.rs
+
0
−
72
View file @
11243358
...
...
@@ -6,78 +6,6 @@ use wasm_bindgen::prelude::*;
use
wasm_bindgen
::
JsCast
;
use
web_sys
::{
ErrorEvent
,
MessageEvent
,
WebSocket
};
pub
fn
start_websocket
()
->
Result
<
(),
JsValue
>
{
// Connect to an echo server
let
ws
:
web_sys
::
WebSocket
=
WebSocket
::
new
(
"ws://localhost:9001"
)
?
;
// For small binary messages, like CBOR, Arraybuffer is more efficient than Blob handling
ws
.set_binary_type
(
web_sys
::
BinaryType
::
Arraybuffer
);
// create callback
let
cloned_ws
=
ws
.clone
();
let
onmessage_callback
=
Closure
::
wrap
(
Box
::
new
(
move
|
e
:
MessageEvent
|
{
// Handle difference Text/Binary,...
if
let
Ok
(
abuf
)
=
e
.data
()
.dyn_into
::
<
js_sys
::
ArrayBuffer
>
()
{
info!
(
"message event, received arraybuffer: {:?}"
,
abuf
);
let
array
=
js_sys
::
Uint8Array
::
new
(
&
abuf
);
let
len
=
array
.byte_length
()
as
usize
;
info!
(
"Arraybuffer received {}bytes: {:?}"
,
len
,
array
.to_vec
());
// here you can for example use Serde Deserialize decode the message
// for demo purposes we switch back to Blob-type and send off another binary message
cloned_ws
.set_binary_type
(
web_sys
::
BinaryType
::
Blob
);
match
cloned_ws
.send_with_u8_array
(
&
vec!
[
5
,
6
,
7
,
8
])
{
Ok
(
_
)
=>
info!
(
"binary message successfully sent"
),
Err
(
err
)
=>
info!
(
"error sending message: {:?}"
,
err
),
}
}
else
if
let
Ok
(
blob
)
=
e
.data
()
.dyn_into
::
<
web_sys
::
Blob
>
()
{
info!
(
"message event, received blob: {:?}"
,
blob
);
// better alternative to juggling with FileReader is to use https://crates.io/crates/gloo-file
let
fr
=
web_sys
::
FileReader
::
new
()
.unwrap
();
let
fr_c
=
fr
.clone
();
// create onLoadEnd callback
let
onloadend_cb
=
Closure
::
wrap
(
Box
::
new
(
move
|
_e
:
web_sys
::
ProgressEvent
|
{
let
array
=
js_sys
::
Uint8Array
::
new
(
&
fr_c
.result
()
.unwrap
());
let
len
=
array
.byte_length
()
as
usize
;
info!
(
"Blob received {}bytes: {:?}"
,
len
,
array
.to_vec
());
// here you can for example use the received image/png data
})
as
Box
<
dyn
FnMut
(
web_sys
::
ProgressEvent
)
>
);
fr
.set_onloadend
(
Some
(
onloadend_cb
.as_ref
()
.unchecked_ref
()));
fr
.read_as_array_buffer
(
&
blob
)
.expect
(
"blob not readable"
);
onloadend_cb
.forget
();
}
else
if
let
Ok
(
txt
)
=
e
.data
()
.dyn_into
::
<
js_sys
::
JsString
>
()
{
info!
(
"message event, received Text: {:?}"
,
txt
);
}
else
{
info!
(
"message event, received Unknown: {:?}"
,
e
.data
());
}
})
as
Box
<
dyn
FnMut
(
MessageEvent
)
>
);
// set message event handler on WebSocket
ws
.set_onmessage
(
Some
(
onmessage_callback
.as_ref
()
.unchecked_ref
()));
// forget the callback to keep it alive
onmessage_callback
.forget
();
let
onerror_callback
=
Closure
::
wrap
(
Box
::
new
(
move
|
e
:
ErrorEvent
|
{
info!
(
"error event: {:?}"
,
e
);
})
as
Box
<
dyn
FnMut
(
ErrorEvent
)
>
);
ws
.set_onerror
(
Some
(
onerror_callback
.as_ref
()
.unchecked_ref
()));
onerror_callback
.forget
();
let
cloned_ws
=
ws
.clone
();
let
onopen_callback
=
Closure
::
wrap
(
Box
::
new
(
move
|
_
|
{
info!
(
"socket opened"
);
match
cloned_ws
.send_with_str
(
"ping"
)
{
Ok
(
_
)
=>
info!
(
"message successfully sent"
),
Err
(
err
)
=>
info!
(
"error sending message: {:?}"
,
err
),
}
// send off binary message
match
cloned_ws
.send_with_u8_array
(
&
vec!
[
0
,
1
,
2
,
3
])
{
Ok
(
_
)
=>
info!
(
"binary message successfully sent"
),
Err
(
err
)
=>
info!
(
"error sending message: {:?}"
,
err
),
}
})
as
Box
<
dyn
FnMut
(
JsValue
)
>
);
ws
.set_onopen
(
Some
(
onopen_callback
.as_ref
()
.unchecked_ref
()));
onopen_callback
.forget
();
Ok
(())
}
#[derive(Debug,
Clone,
PartialEq)]
pub
enum
ConnectionStatus
{
Connecting
,
...
...
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