Agora-Chat-API-Examples icon indicating copy to clipboard operation
Agora-Chat-API-Examples copied to clipboard

same message is sent multiple times to the receiver

Open keanyee opened this issue 3 years ago • 4 comments

Same message was sent multiple times to the receiver (userId 1) from sender (userId 1) even though the sender only sent it once. You can see the local time and server time are all the same for the message.

I/flutter (30685): {from: 2, to: 1, body: {type: txt, content: u, translations: {}}, direction: rec, hasRead: false, hasReadAck: false, hasDeliverAck: false, needGroupAck: false, msgId: 1063987074572487632, conversationId: 2, chatType: 0, localTime: 1665293596977, serverTime: 1665293595422, status: 2, isThread: false} I/flutter (30685): {from: 2, to: 1, body: {type: txt, content: u, translations: {}}, direction: rec, hasRead: false, hasReadAck: false, hasDeliverAck: false, needGroupAck: false, msgId: 1063987074572487632, conversationId: 2, chatType: 0, localTime: 1665293596977, serverTime: 1665293595422, status: 2, isThread: false} I/flutter (30685): {from: 2, to: 1, body: {type: txt, content: u, translations: {}}, direction: rec, hasRead: false, hasReadAck: false, hasDeliverAck: false, needGroupAck: false, msgId: 1063987074572487632, conversationId: 2, chatType: 0, localTime: 1665293596977, serverTime: 1665293595422, status: 2, isThread: false} I/flutter (30685): {from: 2, to: 1, body: {type: txt, content: u, translations: {}}, direction: rec, hasRead: false, hasReadAck: false, hasDeliverAck: false, needGroupAck: false, msgId: 1063987074572487632, conversationId: 2, chatType: 0, localTime: 1665293596977, serverTime: 1665293595422, status: 2, isThread: false} I/flutter (30685): {from: 2, to: 1, body: {type: txt, content: u, translations: {}}, direction: rec, hasRead: false, hasReadAck: false, hasDeliverAck: false, needGroupAck: false, msgId: 1063987074572487632, conversationId: 2, chatType: 0, localTime: 1665293596977, serverTime: 1665293595422, status: 2, isThread: false} I/flutter (30685): {from: 2, to: 1, body: {type: txt, content: u, translations: {}}, direction: rec, hasRead: false, hasReadAck: false, hasDeliverAck: false, needGroupAck: false, msgId: 1063987074572487632, conversationId: 2, chatType: 0, localTime: 1665293596977, serverTime: 1665293595422, status: 2, isThread: false}

keanyee avatar Oct 09 '22 05:10 keanyee

@keanyee Do you add the listener multiple times, for example by writing the add listener to the build method?

dujiepeng avatar Oct 09 '22 05:10 dujiepeng

@dujiepeng only once.

  @override
  void initState() {
    super.initState();
    _initSDK();
    _login();
    _addChatListener();
  }
  
  void _addChatListener() {
    ChatClient.getInstance.chatManager.addEventHandler(
        chatUniqueHandlerID,
        ChatEventHandler(onMessagesReceived: onMessagesReceived)
    );
  }
  
 void _sendMessage(String message) async {
    var msg = ChatMessage.createTxtSendMessage(
      targetId: '1',
      content: message,
    );
    msg.setMessageStatusCallBack(
        MessageStatusCallBack(
          onSuccess: () {},
          onError: (e) {
            print(e);
            final errorMessage = "Send message failed, code: ${e.code}, desc: ${e.description}";
            print(errorMessage);
          },
        )
    );
    await ChatClient.getInstance.chatManager.sendMessage(msg);
  }

the onMessagesReceived function is from the flutter example .

keanyee avatar Oct 11 '22 04:10 keanyee

@keanyee this is the only page that has ever executed addEventHandler operation? If there are more than one EMChatEventHandler, all EMChatEventHandler are executed. When dispose of a page, use chatUniqueHandlerID to remove EMChatEventHandler.

dujiepeng avatar Oct 14 '22 09:10 dujiepeng

Add below line in dispose method.

ChatClient.getInstance.chatManager.clearEventHandlers();

darshil123332 avatar Dec 21 '22 04:12 darshil123332