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
/*!
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";
let opened_connections_count = Rc::new(RefCell::new(0));
let received_messages_count = Rc::new(RefCell::new(0));
let peer_generator = || {
let mut server = NetworkManager::new(
SIGNALING_SERVER_URL,
SessionId::new("dummy-session-id".to_string()),
ConnectionType::Stun,
)
.unwrap();
let server_clone = server.clone();
let opened_connections_count = opened_connections_count.clone();
let server_on_open = {
move |user_id| {
console::log_1(&format!("connection to user established: {:?}", user_id).into());
*opened_connections_count.borrow_mut() += 1;
server_clone.send_message(user_id, "ping!");
}
};
let server_clone = server.clone();
let received_messages_count = received_messages_count.clone();
let server_on_message = {
move |user_id, message| {
console::log_1(
&format!(
"server received message from client {:?}: {}",
user_id, message
)
.into(),
);
*received_messages_count.borrow_mut() += 1;
server_clone.send_message(user_id, "pong!");
}
};
server.start(server_on_open, server_on_message).unwrap();
};
peer_generator();
peer_generator();
peer_generator();
```
*/
use crateNetworkManager as OneToManyNetworkManager;
use crateConnectionType;
use ;
use JsValue;
/// 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 [rusty-games-signaling-server](../../rusty_games_signaling_server/index.html) instance,
/// whose full IP address must be provided.
///
/// Startup 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 cloneable pointer to the underlying resource and can be cloned freely.