Skip to content

Commit

Permalink
chore: optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
guoxianzhe committed Nov 21, 2023
1 parent 0f347b3 commit e456620
Show file tree
Hide file tree
Showing 11 changed files with 379 additions and 32 deletions.
318 changes: 318 additions & 0 deletions example/src/advanced/ChannelMetadata/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,318 @@
import {
ChannelInfo,
IStreamChannel,
JoinChannelOptions,
PresenceEvent,
PresenceOptions,
RTM_CHANNEL_TYPE,
RTM_CONNECTION_CHANGE_REASON,
RTM_CONNECTION_STATE,
RTM_ERROR_CODE,
StateItem,
UserState,
} from 'agora-react-native-rtm';
import React, { useCallback, useEffect, useRef, useState } from 'react';

import { ScrollView } from 'react-native';

import { IMetadata } from '../../../../src/IAgoraRtmStorage';
import BaseComponent from '../../components/BaseComponent';
import {
AgoraButton,
AgoraDropdown,
AgoraStyle,
AgoraTextInput,
} from '../../components/ui';
import Config from '../../config/agora.config';
import { useRtmClient } from '../../hooks/useRtmClient';
import { arrayToItems } from '../../utils';
import * as log from '../../utils/log';

export default function ChannelMetadata() {
const [loginSuccess, setLoginSuccess] = useState(false);
const [joinSuccess, setJoinSuccess] = useState(false);
const [streamChannel, setStreamChannel] = useState<IStreamChannel>();
const [cName, setCName] = useState<string>(Config.channelName);
const getChannelMetadataRequestId = useRef<number>();
const setChannelMetadataRequestId = useRef<number>();
const setStateRequestId = useRef<number>();
const getStateRequestId = useRef<number>();
const removeStateRequestId = useRef<number>();
const [uid, setUid] = useState<string>(Config.uid);
const [searchUid, setSearchUid] = useState<string>(Config.uid);
const [feeling, setFeeling] = useState<string>('happy');
const [location, setLocation] = useState<string>('tokyo');

const onJoinResult = useCallback(
(
requestId: number,
channelName: string,
userId: string,
errorCode: RTM_ERROR_CODE
) => {
log.info(
'onJoinResult',
'requestId',
requestId,
'channelName',
channelName,
'userId',
userId,
'errorCode',
errorCode
);
setJoinSuccess(errorCode === RTM_ERROR_CODE.RTM_ERROR_OK);
},
[]
);

const onGetChannelMetadataResult = useCallback(
(
requestId: number,
channelName: string,
channelType: RTM_CHANNEL_TYPE,
data: IMetadata,
errorCode: RTM_ERROR_CODE
) => {
log.info(
'onGetChannelMetadataResult',
'requestId',
requestId,
'channelName',
channelName,
'channelType',
channelType,
'data',
data,
'errorCode',
errorCode
);
if (
requestId === getChannelMetadataRequestId.current &&
errorCode === RTM_ERROR_CODE.RTM_ERROR_OK
) {
log.alert(`${channelName} metadata:`, `${JSON.stringify(data)}`);
}
},
[]
);

/**
* Step 1: getRtmClient and initialize rtm client from BaseComponent
*/
const client = useRtmClient();

/**
* Step 1-1 : createStreamChannel
*/
const createStreamChannel = () => {
if (joinSuccess) {
log.error('already joined channel');
return;
}
let result = client.createStreamChannel(cName);
setStreamChannel(result);
};

/**
* Step 1-2 : join message channel
*/
const join = () => {
if (!streamChannel) {
log.error('please create streamChannel first');
return;
}
streamChannel.join(new JoinChannelOptions({ token: Config.appId }));
};

/**
* Step 1-3 : leave message channel
*/
const leave = () => {
if (streamChannel) {
streamChannel.leave(0);
}
};

/**
* Step 1-4 : destroyStreamChannel
*/
const destroyStreamChannel = useCallback(() => {
streamChannel?.release();
setStreamChannel(undefined);
}, [streamChannel]);

/**
* Step 2 : setChannelMetadata
*/
const setChannelMetadata = () => {
setChannelMetadataRequestId.current = client
.getStorage()
.setChannelMetadata(cName, RTM_CHANNEL_TYPE.RTM_CHANNEL_TYPE_STREAM,data: IMetadata[],
options: MetadataOptions,
lockName: string,);
};

/**
* Step 3 : getChannelMetadata
*/
const getChannelMetadata = () => {
getChannelMetadataRequestId.current = client
.getStorage()
.getChannelMetadata(cName, RTM_CHANNEL_TYPE.RTM_CHANNEL_TYPE_STREAM);
};

/**
* Step 4 : updateChannelMetadata
*/
const updateChannelMetadata = () => {};

/**
* Step 5 : removeChannelMetadata
*/
const removeChannelMetadata = () => {};

useEffect(() => {
client.addEventListener('onJoinResult', onJoinResult);

return () => {
client.removeEventListener('onJoinResult', onJoinResult);
};
}, [client, uid, onJoinResult]);

const onConnectionStateChanged = useCallback(
(
channelName: string,
state: RTM_CONNECTION_STATE,
reason: RTM_CONNECTION_CHANGE_REASON
) => {
log.log(
'onConnectionStateChanged',
'channelName',
channelName,
'state',
state,
'reason',
reason
);
switch (state) {
case RTM_CONNECTION_STATE.RTM_CONNECTION_STATE_CONNECTED:
setLoginSuccess(true);
break;
case RTM_CONNECTION_STATE.RTM_CONNECTION_STATE_DISCONNECTED:
if (
reason ===
RTM_CONNECTION_CHANGE_REASON.RTM_CONNECTION_CHANGED_LOGOUT
) {
setLoginSuccess(false);
destroyStreamChannel();
}
setJoinSuccess(false);
break;
}
},
[destroyStreamChannel]
);
useEffect(() => {
client?.addEventListener(
'onConnectionStateChanged',
onConnectionStateChanged
);
client?.addEventListener(
'onGetChannelMetadataResult',
onGetChannelMetadataResult
);

return () => {
client?.removeEventListener(
'onConnectionStateChanged',
onConnectionStateChanged
);
client?.removeEventListener(
'onGetChannelMetadataResult',
onGetChannelMetadataResult
);
};
}, [client, uid, onConnectionStateChanged, onGetChannelMetadataResult]);

return (
<>
<ScrollView style={AgoraStyle.fullSize}>
<BaseComponent
onChannelNameChanged={(v) => setCName(v)}
onUidChanged={(v) => setUid(v)}
/>
<AgoraButton
disabled={!loginSuccess}
title={`${
streamChannel ? 'destroyStreamChannel' : 'createStreamChannel'
}`}
onPress={() => {
streamChannel ? destroyStreamChannel() : createStreamChannel();
}}
/>
<AgoraButton
disabled={!loginSuccess || !streamChannel}
title={`${joinSuccess ? 'leaveChannel' : 'joinChannel'}`}
onPress={() => {
joinSuccess ? leave() : join();
}}
/>
<AgoraButton
title={`setChannelMetadata`}
onPress={() => {
setChannelMetadata();
}}
/>
<AgoraButton
title={`getChannelMetadata`}
onPress={() => {
getChannelMetadata();
}}
/>
<AgoraButton
title={`updateChannelMetadata`}
onPress={() => {
updateChannelMetadata();
}}
/>
<AgoraButton
title={`removeChannelMetadata`}
onPress={() => {
removeChannelMetadata();
}}
/>
{/* <AgoraTextInput
onChangeText={(text) => {
setSearchUid(text);
}}
label="what uid you want to find"
value={searchUid}
/>
<AgoraButton
title={`whereNow`}
onPress={() => {
whereNow();
}}
/>
<AgoraDropdown
titleStyle={AgoraStyle.dropdownTitle}
title={'what is your location?'}
items={arrayToItems(['tokyo', 'shanghai', 'beijing'])}
value={location}
onValueChange={(value) => {
setLocation(value);
}}
/> */}
{/* <AgoraTextInput
onChangeText={(text) => {
setFeeling(text);
}}
label="How do you feel now?"
value={feeling}
/> */}
</ScrollView>
</>
);
}
5 changes: 5 additions & 0 deletions example/src/advanced/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ChannelMetadata from './ChannelMetadata';
import Presence from './Presence';
import PublishMessage from './publishMessage';
import PublishTopicMessage from './publishTopicMessage';
Expand All @@ -17,6 +18,10 @@ const Advanced = {
name: 'Presence',
component: Presence,
},
{
name: 'ChannelMetadata',
component: ChannelMetadata,
},
],
};
export default Advanced;
26 changes: 12 additions & 14 deletions scripts/terra/config/param_type_list.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"agora::rtm::IMetadata.getMetadataItems.items@type": "MetadataItem*",
"agora::rtm::IChannelMember\\*": "IChannelMember",
"agora::rtm::IRtmClient.createStreamChannel@return_type": "IStreamChannel",
"agora::rtm::IRtmClient.getPresence@return_type": "IRtmPresence",
"^Optional<(.*)>": "$1",
"^util::(.*)": "$1",
"^agora::(.*)": "$1",
Expand All @@ -15,11 +13,11 @@
"^(.*)&": "$1",
"^(.*)\\[(.*)\\]": "$1",

"^view_t\\*$": "any[]",
"^uid_t\\*$": "number",
"^AString$": "string",
"^user_id_t$": "string",
"^view_t$": "any",
"^view_t\\*$": "any[]",
"^uid_t\\*$": "number",
"^uid_t$": "number",
"^track_id_t$": "number",
"^conn_id_t$": "number",
Expand Down Expand Up @@ -47,20 +45,14 @@

"^uint8_t const\\*$": "Uint8Array",
"^uint8_t\\*$": "Uint8Array",
"^uint8_t$": "number",
"^uint32_t const$": "number",
"^uint32_t$": "number",
"^uint64_t$": "number",
"^uint64_t const$": "number",
"^uint16_t$": "number",
"^uint(.*)_t const$": "number",
"^uint(.*)_t$": "number",
"^uint\\*$": "number",
"^uint$": "number",

"^int64_t$": "number",
"^int64_t const$": "number",
"^int32_t\\*$": "number[]",
"^int32_t$": "number",
"^int16_t$": "number",
"^int(.*)_t$": "number",

"^int const\\*$": "number[]",
"^int\\*$": "number[]",
Expand All @@ -73,10 +65,16 @@
"^bool\\*$": "boolean",
"^bool const$": "boolean",
"^bool$": "boolean",
"^(.*)const\\*": "$1[]",

"^(.*)Sink\\*": "$1Sink",
"^(.*)EventHandler\\*": "$1EventHandler",

"^IStreamChannel\\*": "IStreamChannel",
"^IRtmPresence\\*": "IRtmPresence",
"^IRtmStorage\\*": "IRtmStorage",
"^IRtmLock\\*": "IRtmLock",
"^IMetadata const\\*": "IMetadata",
"^(.*)const\\*": "$1[]",

"^(.*)\\*": "$1[]"
}
Loading

0 comments on commit e456620

Please sign in to comment.