Skip to content

Commit 9ce880d

Browse files
fix: remove thread sleep
1 parent e0d5910 commit 9ce880d

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

Runtime/GameEngine/Libraries/WebRtc/webrtc.jslib

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,29 @@ const LibraryWebRtc = {
144144
this.unreliableEnded();
145145
};
146146

147+
this.pendingOfferResolvers = [];
148+
147149
this.createOffer = async iceRestart => {
148150
const offer = await this.pc.createOffer({ iceRestart });
149151
webRtcState.log(`Created offer\n${JSON.stringify(offer)}`);
150152
await this.pc.setLocalDescription(offer);
151-
await new Promise(r => setTimeout(r, webRtcState.offerAnnouncingDelay));
153+
154+
let resolver;
155+
webRtcState.log(`Gathering ICE candidates...`);
156+
await Promise.race([
157+
new Promise(r => setTimeout(r, webRtcState.offerAnnouncingDelay)),
158+
new Promise(r => {
159+
resolver = r;
160+
this.pendingOfferResolvers.push(r);
161+
})
162+
]);
163+
webRtcState.log(`ICE candidates gathered.`);
164+
const index = this.pendingOfferResolvers.indexOf(resolver);
165+
if (index > -1) {
166+
this.pendingOfferResolvers.splice(index, 1);
167+
}
168+
152169
const updatedOffer = this.pc.localDescription;
153-
webRtcState.log(`Updated offer after ${webRtcState.offerAnnouncingDelay} ms\n${JSON.stringify(updatedOffer)}`);
154170
if (this.pc.sctp && this.pc.sctp.transport && this.pc.sctp.transport.iceTransport && typeof this.pc.sctp.transport.iceTransport.getLocalCandidates === 'function') {
155171
webRtcState.log(`Local candidates\n${JSON.stringify(this.pc.sctp.transport.iceTransport.getLocalCandidates())}`);
156172
}
@@ -185,6 +201,10 @@ const LibraryWebRtc = {
185201
iceCandidateCallback(candidateJson);
186202
} else {
187203
webRtcState.log("End of candidates");
204+
while (this.pendingOfferResolvers.length > 0) {
205+
const resolver = this.pendingOfferResolvers.pop();
206+
resolver();
207+
}
188208
iceCandidateCallback(candidate);
189209
}
190210
};

0 commit comments

Comments
 (0)