Skip to content

Commit

Permalink
feat(example): add getOnlineUsers and getUserChannels (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
guoxianzhe authored Nov 23, 2023
1 parent 4e08fcb commit 2bc1363
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
110 changes: 110 additions & 0 deletions example/src/advanced/Presence/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export default function Presence() {
const [loginSuccess, setLoginSuccess] = useState(false);
const [subscribeSuccess, setSubscribeSuccess] = useState(false);
const whoNowRequestId = useRef<number>();
const getOnlineRequestId = useRef<number>();
const getUserChannelsRequestId = useRef<number>();
const whereNowRequestId = useRef<number>();
const setStateRequestId = useRef<number>();
const getStateRequestId = useRef<number>();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -212,13 +276,35 @@ 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
*/
const whereNow = () => {
whereNowRequestId.current = client.getPresence().whereNow(searchUid);
};

/**
* Step 3-1 : getUserChannels
*/
const getUserChannels = () => {
getUserChannelsRequestId.current = client
.getPresence()
.getUserChannels(searchUid);
};

/**
* Step 4 : setState
*/
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -300,7 +396,9 @@ export default function Presence() {
uid,
onSubscribeResult,
onWhoNowResult,
onGetOnlineUsersResult,
onWhereNowResult,
onGetUserChannelsResult,
onPresenceSetStateResult,
onPresenceGetStateResult,
onPresenceRemoveStateResult,
Expand Down Expand Up @@ -372,6 +470,12 @@ export default function Presence() {
whoNow();
}}
/>
<AgoraButton
title={`getOnlineUsers`}
onPress={() => {
getOnlineUsers();
}}
/>
<AgoraTextInput
onChangeText={(text) => {
setSearchUid(text);
Expand All @@ -385,6 +489,12 @@ export default function Presence() {
whereNow();
}}
/>
<AgoraButton
title={`getUserChannels`}
onPress={() => {
getUserChannels();
}}
/>

<AgoraDropdown
titleStyle={AgoraStyle.dropdownTitle}
Expand Down
2 changes: 2 additions & 0 deletions scripts/terra/config/impl_special_return_list.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"agora::rtm::IRtmPresence.setState": "requestId",
"agora::rtm::IRtmPresence.removeState": "requestId",
"agora::rtm::IRtmPresence.getState": "requestId",
"agora::rtm::IRtmPresence.getOnlineUsers": "requestId",
"agora::rtm::IRtmPresence.getUserChannels": "requestId",
"agora::rtm::IRtmLock.setLock": "requestId",
"agora::rtm::IRtmLock.getLocks": "requestId",
"agora::rtm::IRtmLock.removeLock": "requestId",
Expand Down
4 changes: 2 additions & 2 deletions src/impl/IAgoraRtmPresenceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class IRtmPresenceImpl implements IRtmPresence {
},
};
const jsonResults = callIrisApi.call(this, apiType, jsonParams);
return jsonResults.result;
return jsonResults.requestId;
}

protected getApiTypeFromGetOnlineUsers(): string {
Expand All @@ -195,7 +195,7 @@ export class IRtmPresenceImpl implements IRtmPresence {
},
};
const jsonResults = callIrisApi.call(this, apiType, jsonParams);
return jsonResults.result;
return jsonResults.requestId;
}

protected getApiTypeFromGetUserChannels(): string {
Expand Down

0 comments on commit 2bc1363

Please sign in to comment.