Skip to content

Commit

Permalink
feat: useClientEvent add stream-message and `image-moderation-conne…
Browse files Browse the repository at this point in the history
…ction-state-change`
  • Loading branch information
guoxianzhe committed Jun 4, 2024
1 parent 25988b1 commit 48f5c8a
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 95 deletions.
12 changes: 8 additions & 4 deletions examples/basic/src/components/Room.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface RoomProps {
renderRemoteUsers?: () => ReactNode;
micOn: boolean;
cameraOn: boolean;
showUserInfo?: boolean;
}

export function Room({
Expand All @@ -28,6 +29,7 @@ export function Room({
renderAction,
renderLocalUser,
renderRemoteUsers,
showUserInfo = true,
}: RoomProps) {
const isConnected = useIsConnected();

Expand All @@ -46,10 +48,12 @@ export function Room({
return (
<>
{renderAction ? renderAction() : undefined}
<UsersInfo
published={publishedUsers.length + (selfPublished ? 1 : 0)}
total={remoteUsers.length + 1}
/>
{showUserInfo && (
<UsersInfo
published={publishedUsers.length + (selfPublished ? 1 : 0)}
total={remoteUsers.length + 1}
/>
)}
<AutoLayout>
{isConnected &&
(renderLocalUser ? (
Expand Down
5 changes: 5 additions & 0 deletions examples/basic/src/pages/hook/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import UseAutoPlayAudioTrack from "./useAutoPlayAudioTrack";
import UseAutoPlayVideoTrack from "./useAutoPlayVideoTrack";
import UseClientEvent from "./useClientEvent";
import UseConnectionState from "./useConnectionState";
import UseCurrentUID from "./useCurrentUID";
import UseIsConnected from "./useIsConnected";
Expand Down Expand Up @@ -49,6 +50,10 @@ const Hooks = [
label: "useLocalScreenTrack",
component: UseLocalScreenTrack,
},
{
label: "UseClientEvent",
component: UseClientEvent,
},
];

export { Hooks };
86 changes: 86 additions & 0 deletions examples/basic/src/pages/hook/useClientEvent/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import type { ConnectionDisconnectedReason, ConnectionState } from "agora-rtc-react";
import { useClientEvent, useJoin, useRTCClient } from "agora-rtc-react";
import { List, Typography } from "antd";
import { useEffect, useState } from "react";

import { Container, MediaControl, Room } from "../../../components";
import { appConfig } from "../../../utils";

const { Title } = Typography;

export const UseClientEvent = () => {
const [calling, setCalling] = useState(false);
const [logSink, setLogSink] = useState<
Array<{
eventName: string;
value: string;
}>
>([]);
const client = useRTCClient();
useClientEvent(
client,
"connection-state-change",
(
curState: ConnectionState,
revState: ConnectionState,
reason?: ConnectionDisconnectedReason,
) => {
console.log(
`connection-state-change,curState: ${curState},revState: ${revState},reason: ${reason}`,
);
setLogSink(logs =>
logs.concat({
eventName: "connection-state-change",
value: `curState: ${curState},revState: ${revState},reason: ${reason}`,
}),
);
},
);

//local
useJoin(
{
appid: appConfig.appId,
channel: appConfig.channel,
token: appConfig.token,
},
calling,
);
const renderLocalUser = () => {
return <></>;
};

return (
<Container>
<div className="h-screen p-3">
<Title>UseClientEvent</Title>
<List
bordered
dataSource={logSink}
renderItem={item => (
<List.Item>
<Typography.Text mark>[{item.eventName}]</Typography.Text> {item.value}
</List.Item>
)}
style={{ height: "300px", overflow: "auto" }}
/>
</div>
{calling && (
<Room
cameraOn={false}
micOn={false}
renderLocalUser={renderLocalUser}
showUserInfo={false}
/>
)}
<MediaControl
calling={calling}
setCalling={() => {
setCalling(a => !a);
}}
/>
</Container>
);
};

export default UseClientEvent;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"@types/react": "^18.2.14",
"@types/react-dom": "^18.2.7",
"@types/testing-library__jest-dom": "^5.14.8",
"agora-rtc-sdk-ng": "4.20.0",
"agora-rtc-sdk-ng": "4.21.0",
"agora-rtc-sdk-ng-fake": "github:AgoraIO-Extensions/agora-rtc-sdk-ng-fake#semver:^1.0.4",
"react-test-renderer": "^18.2.0",
"storybook": "^7.0.26",
Expand Down
2 changes: 1 addition & 1 deletion packages/agora-rtc-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
},
"main": "src/index.ts",
"devDependencies": {
"agora-rtc-sdk-ng": "4.20.0",
"agora-rtc-sdk-ng": "4.21.0",
"agora-rtc-react-ui": "workspace:*"
},
"release-it": {
Expand Down
43 changes: 43 additions & 0 deletions packages/agora-rtc-react/src/hooks/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ import type { Fn, Nullable } from "../misc/utils";

import { useIsomorphicLayoutEffect } from "./tools";

/**
* Connection state between the SDK and the third-party video moderation service.
*/
export declare enum ImageModerationConnectionState {
/**
* The SDK is connecting to the third-party service.
*/
CONNECTING = "CONNECTING",
/**
* The SDK is reconnecting to the third-party service.
*/
RECONNECTING = "RECONNECTING",
/**
* The SDK is connected to the third-party service.
*/
CONNECTED = "CONNECTED",
/**
* The SDK has disconnected from the third-party service.
*/
CLOSED = "CLOSED",
}

/**
* Occurs when the state of the connection between the SDK and the server changes.
*/
Expand Down Expand Up @@ -413,6 +435,27 @@ export function useClientEvent(
event: "content-inspect-error",
listener: Nullable<(error?: IAgoraRTCError) => void>,
): void;

/**
* @ignore
*/
export function useClientEvent(
client: Nullable<IAgoraRTCClient>,
event: "image-moderation-connection-state-change",
listener: Nullable<
(newState: ImageModerationConnectionState, preState: ImageModerationConnectionState) => void
>,
): void;

/**
* Callback for receiving the DataStream message.
*/
export function useClientEvent(
client: Nullable<IAgoraRTCClient>,
event: "stream-message",
listener: Nullable<(uid: UID, payload: Uint8Array) => void>,
): void;

export function useClientEvent(
client: Nullable<IAgoraRTCClient>,
event: string,
Expand Down
4 changes: 2 additions & 2 deletions packages/agora-rtc-react/src/hooks/useRemoteAudioTracks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function useRemoteAudioTracks(
});

if (!isUnmountRef.current) {
setTracks(nextTracks.current);
setTracks([...nextTracks.current]);
setIsLoading(false);
}
}
Expand All @@ -95,7 +95,7 @@ export function useRemoteAudioTracks(
if (users.some(({ uid }) => user.uid === uid)) {
if (!isUnmountRef.current) {
nextTracks.current = nextTracks.current.filter(track => track.getUserId() !== user.uid);
setTracks(nextTracks.current);
setTracks([...nextTracks.current]);
}
try {
if (!isUnmountRef.current) {
Expand Down
4 changes: 2 additions & 2 deletions packages/agora-rtc-react/src/hooks/useRemoteVideoTracks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function useRemoteVideoTracks(
}
});
if (!isUnmountRef.current) {
setTracks(nextTracks.current);
setTracks([...nextTracks.current]);
setIsLoading(false);
}
}
Expand All @@ -93,7 +93,7 @@ export function useRemoteVideoTracks(
if (users.some(({ uid }) => user.uid === uid)) {
if (!isUnmountRef.current) {
nextTracks.current = nextTracks.current.filter(track => track.getUserId() !== user.uid);
setTracks(nextTracks.current);
setTracks([...nextTracks.current]);
}
try {
if (!isUnmountRef.current) {
Expand Down
23 changes: 23 additions & 0 deletions packages/agora-rtc-react/src/misc/listen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import type {
UID,
} from "agora-rtc-sdk-ng";

import type { ImageModerationConnectionState } from "../hooks";

import type { Disposer, Fn } from "./utils";

export declare enum InspectState {
Expand Down Expand Up @@ -542,6 +544,27 @@ export function listen(
) => void,
): Disposer;

/**
* @ignore
*/
export function listen(
client: IAgoraRTCClient,
event: "image-moderation-connection-state-change",
listener: (
newState: ImageModerationConnectionState,
preState: ImageModerationConnectionState,
) => void,
): Disposer;

/**
* Callback for receiving the DataStream message.
*/
export function listen(
client: IAgoraRTCClient,
event: "stream-message",
listener: (uid: UID, payload: Uint8Array) => void,
): Disposer;

/**
* Occurs when a audio or video track ends.
*
Expand Down
Loading

0 comments on commit 48f5c8a

Please sign in to comment.