Skip to content

Commit

Permalink
refactor(service-providers): add agoraAppId config in join room (#2151
Browse files Browse the repository at this point in the history
)
  • Loading branch information
hyrious authored Jul 12, 2024
1 parent 99d8f12 commit 4ff8041
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/flat-server-api/src/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export interface JoinRoomResult {
rtmToken: string;
showGuide: boolean;
region: Region;
agoraAppId?: string;
billing?: {
/** minutes */
limit: number;
Expand Down
1 change: 1 addition & 0 deletions packages/flat-services/src/services/text-chat/text-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface IServiceTextChatJoinRoomConfig {
ownerUUID: string;
uid: string;
token?: string | null;
agoraAppId?: string | null;
}

export abstract class IServiceTextChat implements IService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface IServiceVideoChatJoinRoomConfig {
shareScreenUID: IServiceVideoChatUID;
shareScreenToken: string;
mirror?: boolean;
agoraAppId?: string | null;
}

export abstract class IServiceVideoChat implements IService {
Expand Down
2 changes: 2 additions & 0 deletions packages/flat-stores/src/classroom-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ export class ClassroomStore {
ownerUUID: this.ownerUUID,
uid: this.userUUID,
token: globalStore.rtmToken,
agoraAppId: globalStore.agoraAppId,
});

const fastboard = await this.whiteboardStore.joinWhiteboardRoom();
Expand Down Expand Up @@ -1508,6 +1509,7 @@ export class ClassroomStore {
shareScreenUID: String(globalStore.rtcShareScreen?.uid || -1),
shareScreenToken: globalStore.rtcShareScreen?.token || "",
mirror: preferencesStore.mirrorMode,
agoraAppId: globalStore.agoraAppId,
});

if (preferencesStore.cameraId) {
Expand Down
3 changes: 3 additions & 0 deletions packages/flat-stores/src/global-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class GlobalStore {
* Room's region, services (currently only whiteboard) must use this value to join the room.
*/
public region: Region | null = null;
public agoraAppId: string | null = null;
public rtcToken: string | null = null;
public rtcUID: number | null = null;
public rtcShareScreen: {
Expand Down Expand Up @@ -219,6 +220,7 @@ export class GlobalStore {
| "rtcUID"
| "rtcShareScreen"
| "region"
| "agoraAppId"
>
>,
): void => {
Expand All @@ -230,6 +232,7 @@ export class GlobalStore {
"rtcUID",
"rtcShareScreen",
"region",
"agoraAppId",
] as const;
for (const key of keys) {
const value = config[key];
Expand Down
2 changes: 2 additions & 0 deletions packages/flat-stores/src/room-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface RoomItem {
expireAt: number;
vipLevel: 0 | 1;
};
agoraAppId?: string;
}

// Only keep sub-room ids. sub-room info are stored in ordinaryRooms.
Expand Down Expand Up @@ -144,6 +145,7 @@ export class RoomStore {
ownerUUID: data.ownerUUID,
roomType: data.roomType,
billing: data.billing,
agoraAppId: data.agoraAppId,
});
return data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ export class AgoraRTCElectron extends IServiceVideoChat {
public readonly isMac: boolean;
public readonly shareScreen = new AgoraRTCElectronShareScreen({ rtc: this });

public readonly APP_ID: string;
public readonly rtcEngine: AgoraSdk;

private readonly _roomSideEffect = new SideEffectManager();

public APP_ID: string;

private _cameraID?: string;
private _micID?: string;
private _speakerID?: string;
Expand Down Expand Up @@ -203,6 +204,12 @@ export class AgoraRTCElectron extends IServiceVideoChat {
}
this.leaveRoom();
}

if (config.agoraAppId && config.agoraAppId !== this.APP_ID) {
this.rtcEngine.release(true);
this.rtcEngine.initialize((this.APP_ID = config.agoraAppId));
}

return this._join(config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ export interface AgoraRTCWebConfig {
}

export class AgoraRTCWeb extends IServiceVideoChat {
public readonly APP_ID: string;

public readonly shareScreen: AgoraRTCWebShareScreen;

private readonly _roomSideEffect = new SideEffectManager();
Expand All @@ -52,6 +50,7 @@ export class AgoraRTCWeb extends IServiceVideoChat {
private _pLeavingRoom?: Promise<unknown>;
private _testingAudio?: HTMLAudioElement;

public APP_ID: string;
public client?: IAgoraRTCClient;
public mode?: IServiceVideoChatMode;

Expand Down Expand Up @@ -358,6 +357,7 @@ export class AgoraRTCWeb extends IServiceVideoChat {
shareScreenUID,
shareScreenToken,
mirror,
agoraAppId,
}: IServiceVideoChatJoinRoomConfig): Promise<void> {
this._roomSideEffect.flushAll();

Expand Down Expand Up @@ -494,6 +494,10 @@ export class AgoraRTCWeb extends IServiceVideoChat {
}),
);

if (agoraAppId && agoraAppId !== this.APP_ID) {
this.APP_ID = agoraAppId;
}

await client.join(
this.APP_ID,
roomUUID,
Expand Down
12 changes: 10 additions & 2 deletions service-providers/agora-rtm/src/rtm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ export class AgoraRTM extends IServiceTextChat {
private _pJoiningRoom?: Promise<unknown>;
private _pLeavingRoom?: Promise<unknown>;

public readonly client: RtmClient;
public client: RtmClient;
public channel?: RtmChannel;

private roomUUID?: string;
private userUUID?: string;
private token?: string;

public constructor(APP_ID: string) {
public constructor(public APP_ID: string) {
super();
if (!APP_ID) {
throw new Error("APP_ID is not set");
Expand Down Expand Up @@ -185,13 +185,21 @@ export class AgoraRTM extends IServiceTextChat {
token,
roomUUID,
ownerUUID,
agoraAppId,
}: IServiceTextChatJoinRoomConfig): Promise<void> {
this.token = token || (await generateRTMToken());

if (!this.token) {
throw new Error("Missing Agora RTM token");
}

if (agoraAppId && agoraAppId !== this.APP_ID) {
this.APP_ID = agoraAppId;
this.client = RtmEngine.createInstance(this.APP_ID, {
logFilter: RtmEngine.LOG_FILTER_WARNING,
});
}

this._roomSideEffect.add(() => {
const handler = async (): Promise<void> => {
this.token = await generateRTMToken();
Expand Down
7 changes: 6 additions & 1 deletion service-providers/agora-rtm2/src/rtm2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class AgoraRTM2 extends IServiceTextChat {
private userUUID?: string;
private token?: string;

public constructor(private readonly APP_ID: string) {
public constructor(public APP_ID: string) {
super();
if (!APP_ID) {
throw new Error("APP_ID is not set");
Expand Down Expand Up @@ -162,13 +162,18 @@ export class AgoraRTM2 extends IServiceTextChat {
token,
roomUUID,
ownerUUID,
agoraAppId,
}: IServiceTextChatJoinRoomConfig): Promise<void> {
this.token = token || (await generateRTMToken());

if (!this.token) {
throw new Error("Missing Agora RTM token");
}

if (agoraAppId && agoraAppId !== this.APP_ID) {
this.APP_ID = agoraAppId;
}

const client = (this.client = new RTM(this.APP_ID, uid, {
logLevel: "warn",
logUpload: !process.env.DEV,
Expand Down

0 comments on commit 4ff8041

Please sign in to comment.