1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*!
Library module for implementation of the many-to-many topology of peer communication.
Each peer in session is an equal, with ability to send and receive messages from any other peer.
Unlike with one-to-many topology, any peer can leave at any time without compromising the network.
To identify peers you should store [`UserId`] accessible inside `on_open_callback` in some custom structure.
Then you can use it in [`NetworkManager::send_message`] to specify exactly which peer should receive the message.
# Example
In this example we create 3 peers that all establish connection with each other.
Each of the peers will send a `ping` message to each new connection.
Also each peer will respond with a `pong` response.
Overall we will expect 6 `ping` and 6 `pong` messages (3 connections, both peers in each).
```
use wasm_peers::many_to_many::NetworkManager;
use wasm_peers::{ConnectionType, SessionId};
use std::cell::RefCell;
use std::rc::Rc;
use web_sys::console;
// there should be a signaling server from accompanying crate listening on this port
const SIGNALING_SERVER_URL: &str = "ws://0.0.0.0:9001/one-to-many";
const STUN_SERVER_URL: &str = "stun:openrelay.metered.ca:80";
let peer_generator = || {
let mut peer = NetworkManager::new(
SIGNALING_SERVER_URL,
SessionId::new("dummy-session-id".to_string()),
ConnectionType::Stun { urls: STUN_SERVER_URL.to_string() },
)
.unwrap();
let peer_clone = peer.clone();
let peer_on_open = {
move |user_id| {
console::log_1(&format!("connection to peer established: {:?}", user_id).into());
peer_clone.send_message(user_id, "ping!");
}
};
let peer_clone = peer.clone();
let peer_on_message = {
move |user_id, message| {
console::log_1(
&format!(
"peer received message from other peer {:?}: {}",
user_id, message
)
.into(),
);
peer_clone.send_message(user_id, "pong!");
}
};
peer.start(peer_on_open, peer_on_message);
};
peer_generator();
peer_generator();
peer_generator();
```
*/
use JsValue;
use ;
use crateNetworkManager as OneToManyNetworkManager;
use crateConnectionType;
/// Abstraction over `WebRTC` peer-to-peer connection.
/// Structure representing equal peer in many-to-many topology.
///
/// `WebRTC` data channel communication abstracted to a single class.
/// All setup is handled internally, you must only provide callbacks
/// for when the connection opens and for handling incoming messages.
/// It also provides a method of sending data to the other end of the connection.
///
/// Only works with [wasm-peers-signaling-server](https://docs.rs/wasm-peers-signaling-server/latest/wasm_peers_signaling_server/) instance,
/// whose full address must be provided.
///
/// Start-up flow is divided into two methods [`NetworkManager::new`] and [`NetworkManager::start`]
/// to allow possibility of referring to network manger itself from the callbacks.
///
/// This class is a pointer to the underlying resource and can be cloned freely.