Skip to content

Commit 4557be6

Browse files
committed
Support sending data between clients over signalling transport
1 parent b327379 commit 4557be6

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

client/tiny.js

+2
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ async function attachPublisher(session) {
151151
this.addUser(session, data.user_id);
152152
} else if (data.event == "leave" && data.room_id == roomId) {
153153
this.removeUser(session, data.user_id);
154+
} else if (data.event == "data") {
155+
console.log(data);
154156
}
155157
});
156158

docs/api.md

+13
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,17 @@ Unblock a user who you previously blocked. That user will get an `unblocked` eve
118118
}
119119
```
120120

121+
### Data
122+
123+
Sends a data payload string to all other users in the room, or to a specific user in the room. Useful for reliable
124+
cross-client communication within a room without having to set up a WebRTC data channel.
125+
126+
```
127+
{
128+
"kind": "data",
129+
"whom": [none|user ID]
130+
"body": string
131+
}
132+
```
133+
121134
[janus-transports]: https://janus.conf.meetecho.com/docs/rest.html

src/lib.rs

+48-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,36 @@ lazy_static! {
136136
};
137137
}
138138

139+
// todo: clean up duplication here
140+
141+
fn send_data_user<T: IntoIterator<Item=U>, U: AsRef<Session>>(json: &JsonValue, target: &UserId, everyone: T) {
142+
let receivers = everyone.into_iter().filter(|s| {
143+
let subscription_state = s.as_ref().subscription.get();
144+
let join_state = s.as_ref().join_state.get();
145+
match (subscription_state, join_state) {
146+
(Some(subscription), Some(joined)) => {
147+
subscription.data && &joined.user_id == target
148+
}
149+
_ => false
150+
}
151+
});
152+
send_message(json, receivers)
153+
}
154+
155+
fn send_data_except<T: IntoIterator<Item=U>, U: AsRef<Session>>(json: &JsonValue, myself: &UserId, everyone: T) {
156+
let receivers = everyone.into_iter().filter(|s| {
157+
let subscription_state = s.as_ref().subscription.get();
158+
let join_state = s.as_ref().join_state.get();
159+
match (subscription_state, join_state) {
160+
(Some(subscription), Some(joined)) => {
161+
subscription.data && &joined.user_id != myself
162+
}
163+
_ => false
164+
}
165+
});
166+
send_message(json, receivers)
167+
}
168+
139169
fn notify_user<T: IntoIterator<Item=U>, U: AsRef<Session>>(json: &JsonValue, target: &UserId, everyone: T) {
140170
let notifiees = everyone.into_iter().filter(|s| {
141171
let subscription_state = s.as_ref().subscription.get();
@@ -465,12 +495,29 @@ fn process_subscribe(from: &Arc<Session>, what: Subscription) -> MessageResult {
465495
Ok(MessageResponse::msg(json!({})))
466496
}
467497

498+
fn process_data(from: &Arc<Session>, whom: Option<UserId>, body: String) -> MessageResult {
499+
let payload = json!({ "event": "data", "body": body });
500+
let switchboard = STATE.switchboard.write()?;
501+
if let Some(joined) = from.join_state.get() {
502+
let occupants = switchboard.occupants_of(&joined.room_id);
503+
if let Some(user_id) = whom {
504+
send_data_user(&payload, &user_id, occupants);
505+
} else {
506+
send_data_except(&payload, &joined.user_id, occupants);
507+
}
508+
Ok(MessageResponse::msg(json!({})))
509+
} else {
510+
Err(From::from("Cannot send data when not in a room."))
511+
}
512+
}
513+
468514
fn process_message(from: &Arc<Session>, msg: MessageKind) -> MessageResult {
469515
match msg {
516+
MessageKind::Join { room_id, user_id, subscribe } => process_join(from, room_id, user_id, subscribe),
470517
MessageKind::Subscribe { what } => process_subscribe(from, what),
471518
MessageKind::Block { whom } => process_block(from, whom),
472519
MessageKind::Unblock { whom } => process_unblock(from, whom),
473-
MessageKind::Join { room_id, user_id, subscribe } => process_join(from, room_id, user_id, subscribe),
520+
MessageKind::Data { whom, body } => process_data(from, whom, body),
474521
}
475522
}
476523

src/messages.rs

+6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ pub enum MessageKind {
7171

7272
/// Undoes a block targeting the given user.
7373
Unblock { whom: UserId },
74+
75+
/// Sends arbitrary data to either all other clients in the room with you, or to a single other client.
76+
Data {
77+
whom: Option<UserId>,
78+
body: String
79+
}
7480
}
7581

7682
/// Information about which traffic a client will get pushed to them.

0 commit comments

Comments
 (0)