服务端处理:
- 参考DatabaseStore 中的
MessageBundle getMessage(long messageId)
方法,获取到 WFCMessage.MessageContent
- 通过OutputMessageData 中的
OutputMessageData fromProtoMessage(WFCMessage.Message protoMessage, OutputClient fromClient)
将WFCMessage.MessageContent
转换成OutputMessageData
- 将
OutputMessageData
返回给前端,可以通过 json 等格式
Web 处理:
- 拿到从服务端返回的
OutputMessageData
- 参考
message.js
里面的fromProtoMessage
方法,该方法的参数obj
和上面的OutputMessageData
非常类似,但还是有一点区别,请参考下面的方法:
static fromOutputMessageData(obj) {
let msg = new Message();
msg.from = obj.sender;
msg.content = obj.payload;
msg.messageUid = obj.messageId;
msg.timestamp = obj.timestamp;
let contentClazz = MessageConfig.getMessageContentClazz(obj.payload.type);
if (contentClazz) {
let content = new contentClazz();
try {
// outputMessageData.payload.base64edData 已经是base64 格式
obj.payload.binaryContent = obj.payload.base64edData;
delete obj.payload.base64edData;
content.decode(obj.payload);
content.extra = obj.payload.extra;
if (content instanceof NotificationMessageContent) {
content.fromSelf = msg.from === wfc.getUserId();
}
} catch (error) {
console.error('decode message payload failed, fallback to unkownMessage', obj.payload, error);
let flag = MessageConfig.getMessageContentPersitFlag(obj.payload.type);
if (PersistFlag.Persist === flag || PersistFlag.Persist_And_Count === flag) {
content = new UnknownMessageContent(obj.payload);
} else {
return null;
}
}
msg.messageContent = content;
if (content instanceof UnknownMessageContent) {
console.log('unknownMessage Content', obj)
}
} else {
console.error('message content not register', obj);
}
if (msg.from === wfc.getUserId()) {
msg.conversation = new Conversation(obj.conv.type, obj.conv.target, obj.conv.line);
// out
msg.direction = 0;
msg.status = MessageStatus.Sent;
} else {
if (obj.conv.type === ConversationType.Single) {
msg.conversation = new Conversation(obj.conv.type, obj.fromUser, obj.conv.line);
} else {
msg.conversation = new Conversation(obj.conv.type, obj.conv.target, obj.conv.line);
}
// in
msg.direction = 1;
// 无法计算已读状态
// msg.status = MessageStatus.Unread;
//
// if (msg.content.mentionedType === 2) {
// msg.status = MessageStatus.AllMentioned;
// } else if (msg.content.mentionedType === 1) {
// for (const target of msg.content.mentionedTarget) {
// if (target === wfc.getUserId()) {
// msg.status = MessageStatus.Mentioned;
// break;
// }
// }
// }
}
return msg;
}