From 2bc13639b0f92f3810ec5d4b6a2424beb1a82122 Mon Sep 17 00:00:00 2001 From: guoxianzhe <53285945+guoxianzhe@users.noreply.github.com> Date: Thu, 23 Nov 2023 12:14:27 +0800 Subject: [PATCH] feat(example): add getOnlineUsers and getUserChannels (#4) --- example/src/advanced/Presence/index.tsx | 110 ++++++++++++++++++ .../config/impl_special_return_list.json | 2 + src/impl/IAgoraRtmPresenceImpl.ts | 4 +- 3 files changed, 114 insertions(+), 2 deletions(-) diff --git a/example/src/advanced/Presence/index.tsx b/example/src/advanced/Presence/index.tsx index 801b15f..b8d7a25 100644 --- a/example/src/advanced/Presence/index.tsx +++ b/example/src/advanced/Presence/index.tsx @@ -29,6 +29,8 @@ export default function Presence() { const [loginSuccess, setLoginSuccess] = useState(false); const [subscribeSuccess, setSubscribeSuccess] = useState(false); const whoNowRequestId = useRef(); + const getOnlineRequestId = useRef(); + const getUserChannelsRequestId = useRef(); const whereNowRequestId = useRef(); const setStateRequestId = useRef(); const getStateRequestId = useRef(); @@ -89,6 +91,40 @@ export default function Presence() { [cName] ); + const onGetOnlineUsersResult = useCallback( + ( + requestId: number, + userStateList: UserState[], + count: number, + nextPage: string, + errorCode: RTM_ERROR_CODE + ) => { + log.log( + 'onGetOnlineUsersResult', + 'requestId', + requestId, + 'userStateList', + userStateList, + 'count', + count, + 'nextPage', + nextPage, + 'errorCode', + errorCode + ); + if ( + requestId === getOnlineRequestId.current && + errorCode === RTM_ERROR_CODE.RTM_ERROR_OK + ) { + log.alert( + `channel: ${cName} status`, + `${JSON.stringify(userStateList)}` + ); + } + }, + [cName] + ); + const onWhereNowResult = useCallback( ( requestId: number, @@ -117,6 +153,34 @@ export default function Presence() { [searchUid] ); + const onGetUserChannelsResult = useCallback( + ( + requestId: number, + channels: ChannelInfo[], + count: number, + errorCode: RTM_ERROR_CODE + ) => { + log.log( + 'onGetUserChannelsResult', + 'requestId', + requestId, + 'channels', + channels, + 'count', + count, + 'errorCode', + errorCode + ); + if ( + requestId === getUserChannelsRequestId.current && + errorCode === RTM_ERROR_CODE.RTM_ERROR_OK + ) { + log.alert(`${searchUid} is at`, `${JSON.stringify(channels)}`); + } + }, + [searchUid] + ); + const onPresenceSetStateResult = useCallback( (requestId: number, errorCode: RTM_ERROR_CODE) => { log.log( @@ -212,6 +276,19 @@ export default function Presence() { ); }; + /** + * Step 2-1 : getOnlineUsers + */ + const getOnlineUsers = () => { + getOnlineRequestId.current = client + .getPresence() + .getOnlineUsers( + cName, + RTM_CHANNEL_TYPE.RTM_CHANNEL_TYPE_MESSAGE, + new PresenceOptions({ includeState: true, includeUserId: true }) + ); + }; + /** * Step 3 : whereNow */ @@ -219,6 +296,15 @@ export default function Presence() { whereNowRequestId.current = client.getPresence().whereNow(searchUid); }; + /** + * Step 3-1 : getUserChannels + */ + const getUserChannels = () => { + getUserChannelsRequestId.current = client + .getPresence() + .getUserChannels(searchUid); + }; + /** * Step 4 : setState */ @@ -262,6 +348,8 @@ export default function Presence() { useEffect(() => { client.addEventListener('onSubscribeResult', onSubscribeResult); client.addEventListener('onWhoNowResult', onWhoNowResult); + client.addEventListener('onGetOnlineUsersResult', onGetOnlineUsersResult); + client.addEventListener('onGetUserChannelsResult', onGetUserChannelsResult); client.addEventListener('onWhereNowResult', onWhereNowResult); client.addEventListener( 'onPresenceSetStateResult', @@ -280,6 +368,14 @@ export default function Presence() { return () => { client.removeEventListener('onSubscribeResult', onSubscribeResult); client.removeEventListener('onWhoNowResult', onWhoNowResult); + client.removeEventListener( + 'onGetOnlineUsersResult', + onGetOnlineUsersResult + ); + client.removeEventListener( + 'onGetUserChannelsResult', + onGetUserChannelsResult + ); client.removeEventListener('onWhereNowResult', onWhereNowResult); client.removeEventListener( 'onPresenceSetStateResult', @@ -300,7 +396,9 @@ export default function Presence() { uid, onSubscribeResult, onWhoNowResult, + onGetOnlineUsersResult, onWhereNowResult, + onGetUserChannelsResult, onPresenceSetStateResult, onPresenceGetStateResult, onPresenceRemoveStateResult, @@ -372,6 +470,12 @@ export default function Presence() { whoNow(); }} /> + { + getOnlineUsers(); + }} + /> { setSearchUid(text); @@ -385,6 +489,12 @@ export default function Presence() { whereNow(); }} /> + { + getUserChannels(); + }} + />