Skip to content

Commit

Permalink
fix(web): force wait rtc join (#1036)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrious authored Oct 26, 2021
1 parent d1dc852 commit b3ea971
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion web/flat-web/src/api-middleware/rtc/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export enum RtcChannelType {
export class RtcRoom {
public client?: IAgoraRTCClient;

private resolveJoined!: () => void;
private joined = new Promise<void>(resolve => {
this.resolveJoined = resolve;
});
private roomUUID?: string;

public async join({
Expand All @@ -49,7 +53,7 @@ export class RtcRoom {
channelType: RtcChannelType;
}): Promise<IAgoraRTCClient> {
if (this.client) {
await this.destroy();
return this.client;
}

const mode = channelType === RtcChannelType.Communication ? "rtc" : "live";
Expand All @@ -67,6 +71,7 @@ export class RtcRoom {
this.client.on("user-published", this.onUserPublished);
this.client.on("user-unpublished", this.onUserUnpublished);
await this.client.join(AGORA.APP_ID, roomUUID, token, rtcUID);
this.resolveJoined();

this.roomUUID = roomUUID;

Expand All @@ -79,6 +84,7 @@ export class RtcRoom {

public async destroy(): Promise<void> {
if (this.client) {
await this.joined;
setMicrophoneTrack();
setCameraTrack();
if (this.client.localTracks.length > 0) {
Expand Down Expand Up @@ -111,6 +117,7 @@ export class RtcRoom {

public async getLocalAudioTrack(): Promise<ILocalAudioTrack> {
if (!this._localAudioTrack) {
await this.joined;
this._localAudioTrack = await AgoraRTC.createMicrophoneAudioTrack();
setMicrophoneTrack(this._localAudioTrack as IMicrophoneAudioTrack);
this._localAudioTrack.once("track-ended", () => {
Expand All @@ -124,6 +131,7 @@ export class RtcRoom {

public async getLocalVideoTrack(): Promise<ILocalVideoTrack> {
if (!this._localVideoTrack) {
await this.joined;
this._localVideoTrack = await AgoraRTC.createCameraVideoTrack({
encoderConfig: { width: 288, height: 216 },
});
Expand Down

0 comments on commit b3ea971

Please sign in to comment.