GoEasyIM - 群聊

初始化

微信小程序特别提醒:

如果需要在小程序里使用IM聊天,需要登录微信公众平台->微信小程序开发设置->服务器域名, 添加socket合法域名:wss://wx-hangzhou.goeasy.io


    var options = {
        host:'hangzhou.goeasy.io', //应用所在的区域地址: [hangzhou.goeasy.io, 新加坡暂不支持IM,敬请期待]
        appkey: "您的common key"            
    }

    //初始化
    var im = GoEasyIM.getInstance(options);

    var user = {
        id:'user001',
        data:'{"avatar":"/www/xxx.png","nickname":"Neo"}' //当前用户的扩展数据, 任意格式的字符串或者对象,用于会话列表conversation.data和群消息的senderData
    }

    //连接GoEasy
    var promise = im.connect(user);

    promise.then(function () {
        console.log("Connection successfully.");
    }).catch(function (error) {
        console.log("Failed to connect GoEasy, code:" + error.code + " content:" + error.content);
    });

发送群消息


    //创建消息, 内容最长不超过3K,可以发送字符串,对象和json格式字符串
    let textMessage = im.createTextMessage({
        text:'Hello, GoEasyIM', //消息内容
        to : {
            type : GoEasyIM.SCENE.GROUP,   //私聊还是群聊,私聊为GoEasyIM.SCENE.PRIVATE
            id : 'group001',  //群id
            data : '{"avatar":"/www/xxx.png","nickname":"区块链交流群"}' //群信息, 任意格式的字符串或者对象,用于更新会话列表中的群信息conversation.data
        }
    });

    //发送消息
    var promise = im.sendMessage(textMessage);


    promise.then(function (message) {
        console.log("Send group message successfully.",message);
    }).catch(function (error) {
        console.log("Failed to send Message, code:" + error.code + " content:" + 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(GoEasyIM.EVENT.GROUP_MESSAGE_RECEIVED, onGroupMessageReceived);


    //订阅群消息
    var groupIds = ["group001"];
    var promise = im.subscribeGroup(groupIds);

    promise.then(function () {
        console.log("Group message subscribe successfully.");
    }).catch(function (error) {
        console.log("Failed to subscribe group message, code:" + error.code + " content:" + error.content);
    });

取消订阅群聊消息


    //取消订阅群聊消息
    var promise = im.unsubscribeGroup("group001");

    promise.then(function () {
        console.log("Group message unsubscribe successfully.");
    }).catch(function (error) {
        console.log("Failed to unsubscribe group message, code:" + error.code + " content:" + error.content);
    });

results matching ""

    No results matching ""