Skip to content
This repository was archived by the owner on Dec 2, 2023. It is now read-only.

Commit 6fc0e09

Browse files
committed
(fix) update example; broadcast
1 parent c12c7d8 commit 6fc0e09

6 files changed

Lines changed: 559 additions & 41 deletions

File tree

apps/room/server/room.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,15 @@ func (r *Room) count() int {
317317
return len(r.peers)
318318
}
319319

320-
func (r *Room) broadcastRoomEvent(event *room.Reply) {
320+
func (r *Room) broadcastRoomEvent(uid string, event *room.Reply) {
321321
log.Infof("event=%+v", event)
322322
peers := r.getPeers()
323323
r.update = time.Now()
324324
for _, p := range peers {
325+
if p.UID() == uid {
326+
continue
327+
}
328+
325329
if err := p.send(event); err != nil {
326330
log.Errorf("send data to peer(%s) error: %v", p.info.Uid, err)
327331
}
@@ -352,6 +356,7 @@ func (r *Room) sendMessage(msg *room.Message) {
352356
log.Debugf("Room.onMessage %v => %v, type: %v, data: %v", from, to, dtype, data)
353357
if to == "all" {
354358
r.broadcastRoomEvent(
359+
from,
355360
&room.Reply{
356361
Payload: &room.Reply_Message{
357362
Message: msg,

apps/room/server/room_service.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (s *RoomService) UpdateRoom(ctx context.Context, in *room.UpdateRoomRequest
138138
}
139139

140140
// broadcast to others
141-
r.broadcastRoomEvent(event)
141+
r.broadcastRoomEvent("", event)
142142
log.Infof("update room ok sid=%v", info.Sid)
143143
return &room.UpdateRoomReply{Success: true}, nil
144144
}
@@ -194,6 +194,7 @@ func (s *RoomService) EndRoom(ctx context.Context, in *room.EndRoomRequest) (*ro
194194
}
195195

196196
r.broadcastRoomEvent(
197+
"",
197198
&room.Reply{
198199
Payload: &room.Reply_Disconnect{
199200
Disconnect: event,
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,30 @@
4444
}
4545
</style>
4646

47-
<title>Pion ion-sfu | Echotest</title>
47+
<title>Pion ion | Echotest</title>
4848
</head>
4949

5050
<body>
5151
<nav class="navbar navbar-light bg-light border-bottom">
5252
<h3>Pion</h3>
5353
</nav>
5454
<div class="container pt-4">
55-
<div class="row" id="start-btns">
55+
<div class="row" id="buttons">
5656
<div class="col-12">
57-
<button type="button" class="btn btn-primary" onclick="start(false)">
58-
start
59-
</button>
60-
<button type="button" class="btn btn-primary" onclick="start(true)">
61-
start with simulcast
62-
</button>
63-
<button type="button" class="btn btn-primary" onclick="joinRoom()">
64-
joinRoom
65-
</button>
66-
<button type="button" class="btn btn-primary" onclick="leaveRoom()">
67-
leaveRoom
68-
</button>
57+
<div class="btn-group" role="group" aria-label="...">
58+
<button id="join-btn" type="button" class="btn btn-primary" onclick="join()">
59+
join
60+
</button>
61+
<button id="publish-btn" type="button" class="btn btn-primary" onclick="start(false)" disabled=true>
62+
publish
63+
</button>
64+
<button id="publish-simulcast-btn" type="button" class="btn btn-primary" onclick="start(true)" disabled=true>
65+
publish(simulcast)
66+
</button>
67+
<button id="leave-btn" type="button" class="btn btn-primary" onclick="leave()" disabled=true>
68+
leave
69+
</button>
70+
</div>
6971
</div>
7072
</div>
7173

@@ -273,8 +275,8 @@ <h3>Pion</h3>
273275
class="d-block border"
274276
style="
275277
background-color: #f8f9fa;
276-
height: 117px;
277-
width: 320px;
278+
height: 120px;
279+
width: 400px;
278280
margin: 5px 0;
279281
"
280282
></pre>
@@ -291,8 +293,8 @@ <h3>Pion</h3>
291293
class="d-block border"
292294
style="
293295
background-color: #f8f9fa;
294-
height: 117px;
295-
width: 320px;
296+
height: 200px;
297+
width: 400px;
296298
margin: 5px 0;
297299
padding: 5px;
298300
"
@@ -308,8 +310,8 @@ <h3>Pion</h3>
308310
class="d-block border"
309311
style="
310312
background-color: #f8f9fa;
311-
height: 117px;
312-
width: 320px;
313+
height: 200px;
314+
width: 400px;
313315
margin: 5px 0;
314316
"
315317
></pre>
@@ -333,9 +335,7 @@ <h3>Pion</h3>
333335
integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV"
334336
crossorigin="anonymous"
335337
></script>
336-
<script src="https://unpkg.com/ion-sdk-js@1.8.0/dist/ion-sdk.min.js"></script>
337-
<script src="https://unpkg.com/ion-sdk-js@1.8.0/dist/ion-connector.min.js"></script>
338-
<!-- <script src="build/bundle.js"></script> -->
338+
<script src="https://unpkg.com/ion-sdk-js@1.8.1/dist/ion-connector.min.js"></script>
339339
<script src="./main.js"></script>
340340
</body>
341341
</html>
Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
const joinBtn = document.getElementById("join-btn");
2+
const leaveBtn = document.getElementById("leave-btn");
3+
const publishBtn = document.getElementById("publish-btn");
4+
const publishSBtn = document.getElementById("publish-simulcast-btn");
5+
16
const localVideo = document.getElementById("local-video");
27
const remoteVideo = document.getElementById("remote-video");
38
const localData = document.getElementById("local-data");
@@ -6,16 +11,17 @@ const sizeTag = document.getElementById("size-tag");
611
const brTag = document.getElementById("br-tag");
712
const codecBox = document.getElementById("select-box1");
813
const resolutionBox = document.getElementById("select-box2");
14+
915
let simulcast = false;
1016
let localDataChannel;
1117

1218
let url = 'http://localhost:5551';
1319
let sid = 'ion';
14-
let uid = "echo-example";
15-
20+
let uid = "local-user";
21+
let connector;
1622
let room;
17-
const joinRoom = async () => {
18-
console.log("[joinRoom]: sid="+sid+" uid=", uid)
23+
const join = async () => {
24+
console.log("[join]: sid="+sid+" uid=", uid)
1925
const connector = new Ion.Connector(url, "token");
2026

2127
connector.onopen = function (service){
@@ -26,7 +32,6 @@ const joinRoom = async () => {
2632
console.log('[onclose]: service = ' + service.name);
2733
};
2834

29-
3035
room = new Ion.Room(connector);
3136

3237
room.onjoin = function (result){
@@ -75,7 +80,6 @@ const joinRoom = async () => {
7580
console.log('[ondisconnect]: Disconnected from server ' + dis);
7681
};
7782

78-
//room.connect();
7983
const result = await room.join({
8084
sid: sid,
8185
uid: uid,
@@ -89,7 +93,12 @@ const joinRoom = async () => {
8993
vendor: 'string',
9094
}, '')
9195
.then((result) => {
92-
console.log('[joinRoom] result: success ' + result?.success + ', room info: ' + JSON.stringify(result?.room));
96+
console.log('[join] result: success ' + result?.success + ', room info: ' + JSON.stringify(result?.room));
97+
joinBtn.disabled = "true";
98+
remoteData.innerHTML = remoteData.innerHTML + JSON.stringify(result) + '\n';
99+
leaveBtn.removeAttribute('disabled');
100+
publishBtn.removeAttribute('disabled');
101+
publishSBtn.removeAttribute('disabled');
93102
});
94103

95104

@@ -104,14 +113,19 @@ const sendMsg = () => {
104113
}
105114
const payload = new Map();
106115
payload.set('msg', localData.value);
107-
console.log("[sendMsg]: sid=", sid, "from=", 'sender', "to=", uid, "payload=", payload);
108-
room.message(sid,'sender', uid, 'Map', payload);
116+
console.log("[sendMsg]: sid=", sid, "from=", uid, "to=", "all", "payload=", payload);
117+
room.message(sid, "sender", "all", 'Map', payload);
109118
}
110119

111120

112-
const leaveRoom = () => {
113-
console.log("[leaveRoom]: sid="+sid+" uid=", uid)
121+
const leave = () => {
122+
console.log("[leave]: sid=" + sid + " uid=", uid)
114123
room.leave(sid, uid);
124+
joinBtn.removeAttribute('disabled');
125+
leaveBtn.disabled = "true";
126+
publishBtn.disabled = "true";
127+
publishSBtn.disabled = "true";
128+
location.reload();
115129
}
116130

117131
remoteVideo.addEventListener("loadedmetadata", function () {
@@ -122,8 +136,7 @@ remoteVideo.onresize = function () {
122136
sizeTag.innerHTML = `${remoteVideo.videoWidth}x${remoteVideo.videoHeight}`;
123137
};
124138

125-
/* eslint-env browser */
126-
const joinBtns = document.getElementById("start-btns");
139+
127140

128141
const local = new Ion.Connector(url);
129142
const remote = new Ion.Connector(url);
@@ -138,12 +151,15 @@ local.onclose = function (service, err) {
138151

139152
const localRTC = new Ion.RTC(local);
140153
const remoteRTC = new Ion.RTC(remote);
154+
let trackEvent;
141155

142156
localRTC.join(sid, uid);
143157

144158
remoteRTC.ontrackevent = function (ev) {
145159
console.log("[ontrackevent]: \nuid = ", ev.uid, " \nstate = ", ev.state, ", \ntracks = ", JSON.stringify(ev.tracks));
146-
event = ev;
160+
if (!trackEvent) {
161+
trackEvent = ev;
162+
}
147163
remoteData.innerHTML = remoteData.innerHTML + JSON.stringify(ev) + '\n';
148164
};
149165

@@ -154,11 +170,15 @@ remoteRTC.ondatachannel = ({ channel }) => {
154170
};
155171
};
156172

157-
remoteRTC.join(sid, "echo-remote");
173+
remoteRTC.join(sid, "remote-user");
158174

159175
let localStream;
160176
const start = (sc) => {
161177
simulcast = sc;
178+
179+
publishSBtn.disabled = "true";
180+
publishBtn.disabled = "true";
181+
162182
Ion.LocalStream.getUserMedia({
163183
resolution: resolutionBox.options[resolutionBox.selectedIndex].value,
164184
codec:codecBox.options[codecBox.selectedIndex].value,
@@ -171,7 +191,7 @@ const start = (sc) => {
171191
localVideo.autoplay = true;
172192
localVideo.controls = true;
173193
localVideo.muted = true;
174-
joinBtns.style.display = "none";
194+
// joinBtns.style.display = "none";
175195
localRTC.publish(media);
176196
localDataChannel = localRTC.createDataChannel("data");
177197
})

0 commit comments

Comments
 (0)