var im = goeasy.im;
//创建消息, 内容最长不超过3K,可以发送字符串,对象和json格式字符串
var textMessage = im.createTextMessage({
text:'Hello, GoEasyIM', //消息内容
to : {
type : GoEasy.IM_SCENE.GROUP, //私聊还是群聊,私聊为GoEasy.IM_SCENE.PRIVATE
id : 'group001', //群id
data : {"avatar":"/www/xxx.png","nickname":"区块链交流群"} //群信息, 任意格式的字符串或者对象,用于更新会话列表中的群信息conversation.data
}
});
//发送消息
im.sendMessage({
message:textMessage,
onSuccess: function (message) { //发送成功
console.log("Group message sent successfully.", message);
},
onFailed: function (error) { //发送失败
console.log("Failed to send group message, code:"+error.code+ ",error:"+error.content);
}
});
对于收到且已读的消息,需要标记为已读状态,否则会话列表里的数字始终保持原来数字,并且在下次连接GoEasy时,GoEasy会重复下发之前未标记的消息。
var onGroupMessageReceived = function(message) {
//群聊消息message示例
// {
// "messageId": "a5f705e0c7e111eab347b726da4416bd",
// "type": "text",
// "timestamp": 1594958255483,
// "senderId": "3bb179af-bcc5-4fe0-9dac-c05688484649",
// "senderData": {"avatar":"/www/xxx.png","nickname":"Neo"}, //发送者Data,仅限群聊消息
// "payload": {
// "text": "Hello, GoEasyIM"
// },
// "groupId": "group-a42b-47b2-bb1e-15e0f5f9a19a"
// }
console.log("received group message:" + JSON.stringify(message));
};
//接收群消息
im.on(GoEasy.IM_EVENT.GROUP_MESSAGE_RECEIVED, onGroupMessageReceived);
//订阅群消息
var groupIds = ["group001"];
im.subscribeGroup({
groupIds:groupIds,
onSuccess: function () { //订阅成功
console.log("Group message subscribe successfully.");
},
onFailed: function (error) { //订阅失败
console.log("Failed to subscribe group message, code:" + error.code + " content:" + error.content);
}
});
//取消订阅群聊消息
im.unsubscribeGroup({
groupId:"group001",
onSuccess: function (message) { //取消成功
console.log("Group message unsubscribe successfully.");
},
onFailed: function (error) { //取消失败
console.log("Failed to unsubscribe group message, code:" + error.code + " content:" + error.content);
}
});