自定义消息,可以根据自己的业务更灵活的扩展消息类型,方便的创建除了文本,图片和视频等以外的其他类型消息。
例如:发送地图坐标、电商客服发送订单、医疗系统发送处方或发送红包等任何业务需要的自定义类型消息。
可以发送一个任意自定义格式的对象,或字符串,但不能包括文件,如需包含文件,只能是文件的路径。
var im = goeasy.im;
//示例:创建一个自定义的类型为'order'订单消息
var order = {
number: '20201818005',
product: 'Mate 30 Pro',
image: 'https://n.sinaimg.cn/sinacn10123/200/w640h360/20190916/e3d2-ietnfsp7076350.jpg',
price: 3999
};
var customMessage = im.createCustomMessage({
type:'order', //字符串,可以任意自定义类型,比如红包'hongbao', 订单'order,处方'chufang'
payload: order,
to : {
type : GoEasy.IM_SCENE.PRIVATE, //私聊还是群聊,群聊为GoEasy.IM_SCENE.GROUP
id : 'user002',
data:{"avatar":"/www/xxx.png","nickname":"Neo"} //好友扩展数据, 任意格式的字符串或者对象,用于更新会话列表conversation.data
}
});
//发送消息
im.sendMessage({
message:customMessage,
onSuccess: function (message) { //发送成功
console.log('Message sent successfully.',message);
},
onFailed: function (error) { //发送失败
console.log('Failed to send message,code:' + error.code +',error'+error.content);
}
});
var onPrivateMessageReceived = function(message) {
//示例:收到一条自定义类型为‘order’的订单消息,
// {
// "messageId": "8f0e27a0c7e111eab347b726da4416bd",
// "timestamp": 1594958217087,
// "type": "order", //自定义类型为order
// "senderId": "3bb179af-bcc5-4fe0-9dac-c05688484649",
// "payload": {
// number:'20201818005',
// product:'Mate 30 Pro',
// image:'https://n.sinaimg.cn/sinacn10123/200/w640h360/20190916/e3d2-ietnfsp7076350.jpg',
// price: 3999
// },
// "receiverId": "fdee46b0-4b01-4590-bdba-6586d7617f95"
// }
console.log('收到消息:senderId:' + message.senderId +', content:' + message.payload);
};
//监听和接收单聊消息
im.on(GoEasy.IM_EVENT.PRIVATE_MESSAGE_RECEIVED, onPrivateMessageReceived);