|
|
@@ -1,6 +1,7 @@
|
|
|
import { useState, useEffect, useRef } from 'react';
|
|
|
import { useParams } from 'react-router';
|
|
|
import { User } from '../../../share/types.ts';
|
|
|
+import { TokenAPI } from '../../api.ts';
|
|
|
// @ts-types="../../../share/aliyun-rtc-sdk.d.ts"
|
|
|
import AliRtcEngine, { AliRtcSubscribeState, AliRtcVideoTrack } from 'aliyun-rtc-sdk';
|
|
|
import { toast } from 'react-toastify';
|
|
|
@@ -67,12 +68,6 @@ export enum ClassStatus {
|
|
|
ENDED = 'ended'
|
|
|
}
|
|
|
|
|
|
-// 配置信息
|
|
|
-const IM_APP_ID = '4c2ab5e1b1b0';
|
|
|
-const IM_APP_KEY = '314bb5eee5b623549e8a41574ba3ff32';
|
|
|
-const IM_APP_SIGN = 'H4sIAAAAAAAE/wCQAG//zguHB+lYCilkv7diSkk4GmcvLuds+InRu9vFOFebMwm/jEgsK5bBT85Z0owObMxG58uXHyPFlPEBEDQm9FswNJ+KmX0VDYkcfdPPWkafA6Hc0B6F+p5De9yJfPEfHzwo/DHMaygbHfLmBgUtmKveq421sJr/gNBz9D04Ewsg39us+ao0NegzLt7xtXvFXXXJAAAA//8BAAD//yoav6aQAAAA';
|
|
|
-const RTC_APP_ID = 'a5842c2a-d94a-43be-81de-1fdb712476e1';
|
|
|
-const RTC_APP_KEY = 'b71d65f4f84c450f6f058f4ad507bd42';
|
|
|
|
|
|
export const useClassroom = ({ user }:{ user : User }) => {
|
|
|
// 状态管理
|
|
|
@@ -114,41 +109,8 @@ export const useClassroom = ({ user }:{ user : User }) => {
|
|
|
toast[type](message);
|
|
|
};
|
|
|
|
|
|
- const hex = (buffer: ArrayBuffer): string => {
|
|
|
- const hexCodes = [];
|
|
|
- const view = new DataView(buffer);
|
|
|
- for (let i = 0; i < view.byteLength; i += 4) {
|
|
|
- const value = view.getUint32(i);
|
|
|
- const stringValue = value.toString(16);
|
|
|
- const padding = '00000000';
|
|
|
- const paddedValue = (padding + stringValue).slice(-padding.length);
|
|
|
- hexCodes.push(paddedValue);
|
|
|
- }
|
|
|
- return hexCodes.join('');
|
|
|
- };
|
|
|
|
|
|
- const generateToken = async (
|
|
|
- appId: string,
|
|
|
- appKey: string,
|
|
|
- channelId: string,
|
|
|
- userId: string,
|
|
|
- timestamp: number
|
|
|
- ): Promise<string> => {
|
|
|
- const encoder = new TextEncoder();
|
|
|
- const data = encoder.encode(`${appId}${appKey}${channelId}${userId}${timestamp}`);
|
|
|
- const hash = await crypto.subtle.digest('SHA-256', data);
|
|
|
- return hex(hash);
|
|
|
- };
|
|
|
|
|
|
- const generateImToken = async (userId: string, role: string): Promise<string> => {
|
|
|
- const nonce = 'AK_4';
|
|
|
- const timestamp = Math.floor(Date.now() / 1000) + 3600 * 3;
|
|
|
- const pendingShaStr = `${IM_APP_ID}${IM_APP_KEY}${userId}${nonce}${timestamp}${role}`;
|
|
|
- const encoder = new TextEncoder();
|
|
|
- const data = encoder.encode(pendingShaStr);
|
|
|
- const hash = await crypto.subtle.digest('SHA-256', data);
|
|
|
- return hex(hash);
|
|
|
- };
|
|
|
|
|
|
// 事件监听函数
|
|
|
const listenImEvents = (): void => {
|
|
|
@@ -163,16 +125,16 @@ export const useClassroom = ({ user }:{ user : User }) => {
|
|
|
showMessage(`IM断开连接: ${code}`);
|
|
|
// 自动重连
|
|
|
try {
|
|
|
- const imToken = await generateImToken(userId, role);
|
|
|
+ const { token, nonce, timestamp } = await TokenAPI.getIMToken(userId, role);
|
|
|
await imEngine.current!.login({
|
|
|
user: {
|
|
|
userId,
|
|
|
userExtension: JSON.stringify(user)
|
|
|
},
|
|
|
userAuth: {
|
|
|
- nonce: 'AK_4',
|
|
|
- timestamp: Math.floor(Date.now() / 1000) + 3600 * 3,
|
|
|
- token: imToken,
|
|
|
+ nonce,
|
|
|
+ timestamp,
|
|
|
+ token,
|
|
|
role
|
|
|
}
|
|
|
});
|
|
|
@@ -442,24 +404,23 @@ export const useClassroom = ({ user }:{ user : User }) => {
|
|
|
|
|
|
try {
|
|
|
const { ImEngine: ImEngineClass } = window.AliVCInteraction;
|
|
|
+ const {appId, appSign, timestamp, nonce, token} = await TokenAPI.getIMToken(userId, role);
|
|
|
imEngine.current = ImEngineClass.createEngine();
|
|
|
await imEngine.current.init({
|
|
|
deviceId: 'xxxx',
|
|
|
- appId: IM_APP_ID,
|
|
|
- appSign: IM_APP_SIGN,
|
|
|
+ appId,
|
|
|
+ appSign,
|
|
|
logLevel: ERROR,
|
|
|
});
|
|
|
-
|
|
|
- const imToken = await generateImToken(userId, role);
|
|
|
await imEngine.current.login({
|
|
|
user: {
|
|
|
userId,
|
|
|
userExtension: JSON.stringify({ nickname: user?.nickname || user?.username || '' })
|
|
|
},
|
|
|
userAuth: {
|
|
|
- nonce: 'AK_4',
|
|
|
- timestamp: Math.floor(Date.now() / 1000) + 3600 * 3,
|
|
|
- token: imToken,
|
|
|
+ nonce,
|
|
|
+ timestamp,
|
|
|
+ token,
|
|
|
role
|
|
|
}
|
|
|
});
|
|
|
@@ -707,8 +668,7 @@ export const useClassroom = ({ user }:{ user : User }) => {
|
|
|
publishScreen = false,
|
|
|
} = publishOptions || {};
|
|
|
|
|
|
- const timestamp = Math.floor(Date.now() / 1000) + 3600 * 3;
|
|
|
- const token = await generateToken(RTC_APP_ID, RTC_APP_KEY, classId, userId, timestamp);
|
|
|
+ const {appId, token, timestamp} = await TokenAPI.getRTCToken(classId, userId);
|
|
|
await aliRtcEngine.current.publishLocalVideoStream(publishVideo);
|
|
|
await aliRtcEngine.current.publishLocalAudioStream(publishAudio);
|
|
|
await aliRtcEngine.current.publishLocalScreenShareStream(publishScreen);
|
|
|
@@ -716,7 +676,7 @@ export const useClassroom = ({ user }:{ user : User }) => {
|
|
|
{
|
|
|
channelId: classId,
|
|
|
userId,
|
|
|
- appId: RTC_APP_ID,
|
|
|
+ appId,
|
|
|
token,
|
|
|
timestamp,
|
|
|
},
|