Open keanyee opened 2 years ago
@keanyee Do you add the listener multiple times, for example by writing the add listener to the build method?
@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
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.
Add below line in dispose method.
ChatClient.getInstance.chatManager.clearEventHandlers();
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}