Skip to content

Commit

Permalink
feat(example): add sent status when send message
Browse files Browse the repository at this point in the history
  • Loading branch information
guoxianzhe committed Nov 24, 2023
1 parent 9f32053 commit e3cf466
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 63 deletions.
70 changes: 40 additions & 30 deletions example/src/advanced/publishMessage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import BaseComponent from '../../components/BaseComponent';
import { AgoraButton, AgoraStyle, AgoraView } from '../../components/ui';
import Config from '../../config/agora.config';
import { useRtmClient } from '../../hooks/useRtmClient';
import { AgoraMessage } from '../../types';
import * as log from '../../utils/log';

export default function PublishMessage() {
Expand All @@ -25,7 +26,7 @@ export default function PublishMessage() {
const [publishMessageByBuffer, setPublishMessageByBuffer] = useState(false);
const [cName, setCName] = useState<string>(Config.channelName);
const [uid, setUid] = useState<string>(Config.uid);
const [messages, setMessages] = useState<any[]>([]);
const [messages, setMessages] = useState<AgoraMessage[]>([]);

const onStorageEvent = useCallback((event: StorageEvent) => {
log.log('onStorageEvent', 'event', event);
Expand Down Expand Up @@ -60,8 +61,17 @@ export default function PublishMessage() {
'errorCode',
errorCode
);
if (errorCode !== RTM_ERROR_CODE.RTM_ERROR_OK) {
log.error('CHANNEL_INVALID_MESSAGE', errorCode);
} else {
messages.map((message) => {
if (message.requestId === requestId) {
message.sent = true;
}
});
}
},
[]
[messages]
);

const onMessageEvent = useCallback(
Expand All @@ -71,7 +81,7 @@ export default function PublishMessage() {
GiftedChat.append(prevState, [
{
_id: +new Date(),
text: event.message,
text: event.message!,
user: {
_id: +new Date(),
name: event.publisher || uid.slice(-1),
Expand All @@ -93,34 +103,34 @@ export default function PublishMessage() {
* Step 2 : publish message to message channel
*/
const publish = useCallback(
(msg: string, msgs: any) => {
let result: number | undefined;
if (publishMessageByBuffer) {
result = client.publishWithBuffer(
cName,
Buffer.from(msg),
msg.length,
new PublishOptions({
type: RTM_MESSAGE_TYPE.RTM_MESSAGE_TYPE_BINARY,
})
);
} else {
result = client.publish(
cName,
msg,
msg.length,
new PublishOptions({
type: RTM_MESSAGE_TYPE.RTM_MESSAGE_TYPE_STRING,
})
);
}

if (result !== RTM_ERROR_CODE.RTM_ERROR_OK) {
log.error('CHANNEL_INVALID_MESSAGE', result);
} else {
(msg: AgoraMessage, msgs: AgoraMessage[]) => {
try {
if (publishMessageByBuffer) {
msg.requestId = client.publishWithBuffer(
cName,
Buffer.from(msg.text),
msg.text?.length,
new PublishOptions({
type: RTM_MESSAGE_TYPE.RTM_MESSAGE_TYPE_BINARY,
})
);
} else {
msg.requestId = client.publish(
cName,
msg.text,
msg.text?.length,
new PublishOptions({
type: RTM_MESSAGE_TYPE.RTM_MESSAGE_TYPE_STRING,
})
);
}
msg.sent = false;
setMessages((previousMessages) =>
GiftedChat.append(previousMessages, msgs)
);
} catch (err) {
log.error(err);
return;
}
},
[cName, client, publishMessageByBuffer]
Expand All @@ -133,8 +143,8 @@ export default function PublishMessage() {
return;
}

msgs.forEach((message: any) => {
publish(message.text, msgs);
msgs.forEach((message: AgoraMessage) => {
publish(message, msgs);
});
},
[loginSuccess, publish]
Expand Down
48 changes: 19 additions & 29 deletions example/src/advanced/publishTopicMessage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from 'agora-react-native-rtm';
import React, { useCallback, useEffect, useState } from 'react';
import { ScrollView } from 'react-native-gesture-handler';
import { GiftedChat } from 'react-native-gifted-chat';
import { GiftedChat, IMessage } from 'react-native-gifted-chat';

import BaseComponent from '../../components/BaseComponent';
import {
Expand All @@ -38,7 +38,7 @@ export default function PublishTopicMessage() {
const [cName, setCName] = useState<string>(Config.channelName);
const [topicName, setTopicName] = useState<string>('topicRTMTest');
const [uid, setUid] = useState<string>(Config.uid);
const [messages, setMessages] = useState<any[]>([]);
const [messages, setMessages] = useState<IMessage[]>([]);

const onSubscribeTopicResult = useCallback(
(
Expand Down Expand Up @@ -176,27 +176,14 @@ export default function PublishTopicMessage() {
[]
);

const onPublishResult = useCallback(
(requestId: number, errorCode: RTM_ERROR_CODE) => {
log.log(
'onPublishResult',
'requestId',
requestId,
'errorCode',
errorCode
);
},
[]
);

const onMessageEvent = useCallback(
(event: MessageEvent) => {
log.log('onMessageEvent', 'event', event);
setMessages((prevState) =>
GiftedChat.append(prevState, [
{
_id: +new Date(),
text: event.message,
text: event.message || '',
user: {
_id: +new Date(),
name: event.publisher || uid.slice(-1),
Expand All @@ -222,27 +209,33 @@ export default function PublishTopicMessage() {
* Step 2 : publish message to topic by publishTopicMessage
*/
const publish = useCallback(
(msg: string, msgs: any[]) => {
(msg: IMessage, msgs: any[]) => {
let result: number | undefined;
msg.sent = false;
if (publishMessageByBuffer) {
result = streamChannel?.publishTopicMessageWithBuffer(
topicName,
Buffer.from(msg),
msg.length,
new PublishOptions({ type: RTM_MESSAGE_TYPE.RTM_MESSAGE_TYPE_BINARY })
Buffer.from(msg.text),
msg.text?.length,
new PublishOptions({
type: RTM_MESSAGE_TYPE.RTM_MESSAGE_TYPE_BINARY,
})
);
} else {
result = streamChannel?.publishTopicMessage(
topicName,
msg,
msg.length,
new PublishOptions({ type: RTM_MESSAGE_TYPE.RTM_MESSAGE_TYPE_STRING })
msg.text,
msg.text?.length,
new PublishOptions({
type: RTM_MESSAGE_TYPE.RTM_MESSAGE_TYPE_STRING,
})
);
}

if (result !== RTM_ERROR_CODE.RTM_ERROR_OK) {
log.error('CHANNEL_INVALID_MESSAGE', result);
log.error('publish topic message failed:', result);
} else {
msg.sent = true;
setMessages((previousMessages) =>
GiftedChat.append(previousMessages, msgs)
);
Expand All @@ -258,8 +251,8 @@ export default function PublishTopicMessage() {
return;
}

msgs.forEach((message: any) => {
publish(message.text, msgs);
msgs.forEach((message: IMessage) => {
publish(message, msgs);
});
},
[loginSuccess, publish]
Expand Down Expand Up @@ -345,7 +338,6 @@ export default function PublishTopicMessage() {
client.addEventListener('onLeaveResult', onLeaveResult);
client.addEventListener('onSubscribeTopicResult', onSubscribeTopicResult);
client.addEventListener('onMessageEvent', onMessageEvent);
client.addEventListener('onPublishResult', onPublishResult);
client.addEventListener('onTopicEvent', onTopicEvent);
client.addEventListener('onJoinTopicResult', onJoinTopicResult);
client.addEventListener('onLeaveTopicResult', onLeaveTopicResult);
Expand All @@ -358,7 +350,6 @@ export default function PublishTopicMessage() {
onSubscribeTopicResult
);
client.removeEventListener('onMessageEvent', onMessageEvent);
client.removeEventListener('onPublishResult', onPublishResult);
client.removeEventListener('onTopicEvent', onTopicEvent);
client.removeEventListener('onJoinTopicResult', onJoinTopicResult);
client.removeEventListener('onLeaveTopicResult', onLeaveTopicResult);
Expand All @@ -368,7 +359,6 @@ export default function PublishTopicMessage() {
uid,
onSubscribeTopicResult,
onMessageEvent,
onPublishResult,
onJoinResult,
onLeaveResult,
onTopicEvent,
Expand Down
5 changes: 4 additions & 1 deletion example/src/basic/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as log from '../../utils/log';
export default function Login() {
const [uid, setUid] = useState(Config.uid);
const [loginSuccess, setLoginSuccess] = useState(false);
const [initResult, setInitResult] = useState<number>(0);
const onLoginResult = useCallback((errorCode: RTM_ERROR_CODE) => {
log.log('onLoginResult', 'errorCode', errorCode);
setLoginSuccess(errorCode === RTM_ERROR_CODE.RTM_ERROR_OK);
Expand Down Expand Up @@ -57,7 +58,7 @@ export default function Login() {
if (!uid || uid.length === 0) {
return;
}
client.initialize(
let result = client.initialize(
new RtmConfig({
userId: uid,
appId: Config.appId,
Expand All @@ -68,6 +69,7 @@ export default function Login() {
},
})
);
setInitResult(result);
return () => {
setLoginSuccess(false);
client.release();
Expand Down Expand Up @@ -123,6 +125,7 @@ export default function Login() {
/>
)}
<AgoraButton
disabled={!uid || initResult !== 0}
title={`${loginSuccess ? 'logout' : 'login'}`}
onPress={() => {
loginSuccess ? logout() : login();
Expand Down
6 changes: 4 additions & 2 deletions example/src/components/BaseComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default function BaseComponent({
onChannelNameChanged,
}: Props) {
const [loginSuccess, setLoginSuccess] = useState(false);
const [initResult, setInitResult] = useState<number>(0);
const [cName, setCName] = useState<string>(Config.channelName);
const [uid, setUid] = useState<string>(Config.uid);

Expand All @@ -37,13 +38,13 @@ export default function BaseComponent({
if (!uid || uid.length === 0) {
return;
}
client.initialize(
let result = client.initialize(
new RtmConfig({
userId: uid,
appId: Config.appId,
})
);

setInitResult(result);
return () => {
setLoginSuccess(false);
client.release();
Expand Down Expand Up @@ -86,6 +87,7 @@ export default function BaseComponent({
disabled={loginSuccess}
/>
<AgoraButton
disabled={!uid || initResult !== 0}
title={`${loginSuccess ? 'logout' : 'login'}`}
onPress={() => {
loginSuccess ? logout() : login();
Expand Down
5 changes: 5 additions & 0 deletions example/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IMessage } from 'react-native-gifted-chat';

export interface AgoraMessage extends IMessage {
requestId?: number;
}
1 change: 0 additions & 1 deletion scripts/terra/config/param_type_list.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"agora::rtm::IMetadata.getMetadataItems@return_type": "MetadataItem*",
"agora::rtm::IChannelMember\\*": "IChannelMember",
"^Optional<(.*)>": "$1",
"^util::(.*)": "$1",
Expand Down

0 comments on commit e3cf466

Please sign in to comment.