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
4565acaf
Unverified
Commit
4565acaf
authored
4 years ago
by
scratchyone
Browse files
Options
Downloads
Patches
Plain Diff
Improve docs
parent
36d086ea
No related branches found
Branches containing commit
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
+15
-15
15 additions, 15 deletions
examples/event.rs
examples/polling.rs
+2
-3
2 additions, 3 deletions
examples/polling.rs
src/lib.rs
+78
-0
78 additions, 0 deletions
src/lib.rs
with
95 additions
and
18 deletions
examples/event.rs
+
15
−
15
View file @
4565acaf
use
console_log
;
use
log
::{
error
,
info
,
Level
};
use
wasm_bindgen
::
JsValue
;
use
wasm_sockets
;
use
console_error_panic_hook
;
use
log
::{
debug
,
error
,
info
,
trace
,
warn
,
Level
};
use
std
::
cell
::
RefCell
;
use
std
::
panic
;
use
std
::
rc
::
Rc
;
use
wasm_bindgen
::
prelude
::
*
;
use
wasm_sockets
;
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"
);
let
mut
client
=
wasm_sockets
::
EventClient
::
new
(
"wss://echo.websocket.org"
)
?
;
client
.set_on_error
(
Some
(
Box
::
new
(|
e
|
{
error!
(
"{:#?}"
,
e
);
client
.set_on_error
(
Some
(
Box
::
new
(|
e
rror
|
{
error!
(
"{:#?}"
,
e
rror
);
})));
client
.set_on_connection
(
Some
(
Box
::
new
(|
c
:
&
wasm_sockets
::
EventClient
,
e
|
{
info!
(
"Connected: {:#?}"
,
e
);
info!
(
"{:#?}"
,
c
.status
);
client
.set_on_connection
(
Some
(
Box
::
new
(|
client
:
&
wasm_sockets
::
EventClient
,
e
|
{
info!
(
"{:#?}"
,
client
.status
);
info!
(
"Sending message..."
);
c
.send_string
(
"
test...
"
)
.unwrap
();
c
.send_binary
(
vec!
[
20
])
.unwrap
();
c
lient
.send_string
(
"
Hello, World!
"
)
.unwrap
();
c
lient
.send_binary
(
vec!
[
20
])
.unwrap
();
})));
client
.set_on_close
(
Some
(
Box
::
new
(||
{
info!
(
"Closed"
);
info!
(
"C
onnection c
losed"
);
})));
client
.set_on_message
(
Some
(
Box
::
new
(
|
c
:
&
wasm_sockets
::
EventClient
,
e
:
wasm_sockets
::
Message
|
{
info!
(
"New Message: {:#?}"
,
e
);
|
c
lient
:
&
wasm_sockets
::
EventClient
,
messag
e
:
wasm_sockets
::
Message
|
{
info!
(
"New Message: {:#?}"
,
messag
e
);
},
)));
...
...
This diff is collapsed.
Click to expand it.
examples/polling.rs
+
2
−
3
View file @
4565acaf
use
console_error_panic_hook
;
use
log
::{
debug
,
error
,
info
,
trace
,
warn
,
Level
};
use
log
::{
info
,
Level
};
use
std
::
cell
::
RefCell
;
use
std
::
panic
;
use
std
::
rc
::
Rc
;
use
wasm_bindgen
::
prelude
::
*
;
use
wasm_bindgen_futures
;
use
wasm_sockets
::{
self
,
ConnectionStatus
};
use
wasm_sockets
::{
self
};
fn
main
()
->
Result
<
(),
JsValue
>
{
panic
::
set_hook
(
Box
::
new
(
console_error_panic_hook
::
hook
));
...
...
This diff is collapsed.
Click to expand it.
src/lib.rs
+
78
−
0
View file @
4565acaf
//! 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.
//! ```
//! use log::{error, info, Level};
//! use wasm_bindgen::JsValue;
//! use wasm_sockets;
//! use console_log;
//!
//! fn main() -> Result<(), JsValue> {
//! // 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");
//!
//! let mut client = wasm_sockets::EventClient::new("wss://echo.websocket.org")?;
//! client.set_on_error(Some(Box::new(|error| {
//! error!("{:#?}", error);
//! })));
//! client.set_on_connection(Some(Box::new(|client: &wasm_sockets::EventClient, e| {
//! info!("{:#?}", client.status);
//! info!("Sending message...");
//! client.send_string("Hello, World!").unwrap();
//! client.send_binary(vec![20]).unwrap();
//! })));
//! client.set_on_close(Some(Box::new(|| {
//! info!("Connection closed");
//! })));
//! client.set_on_message(Some(Box::new(
//! |client: &wasm_sockets::EventClient, message: wasm_sockets::Message| {
//! info!("New Message: {:#?}", message);
//! },
//! )));
//!
//! info!("Connection successfully created");
//! Ok(())
//! }
//! ```
//! The second client offered is the [`PollingClient`]. This client is ideal for games, because it is designed to be used with a loop.
//! 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 log::{info, Level};
//! use std::cell::RefCell;
//! use std::panic;
//! use std::rc::Rc;
//! use wasm_bindgen::prelude::*;
//! use wasm_sockets::{self};
//!
//! fn main() -> Result<(), JsValue> {
//! // 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 || {
//! // receive() gives you all new websocket messages since receive() was last called
//! info!("New messages: {:#?}", client.borrow_mut().receive());
//! }) as Box<dyn FnMut()>);
//!
//! // 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;
//! }
//! ```
use
log
::{
debug
,
error
,
info
,
trace
,
warn
,
Level
};
use
std
::
cell
::
RefCell
;
use
std
::
rc
::
Rc
;
...
...
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