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'
    }

    //连接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);
    });

接收群聊消息


    var onGroupMessageReceived = function(message) {
        //群聊消息message示例
        // {
        //     "messageId": "a5f705e0c7e111eab347b726da4416bd",
        //     "type": "text",
        //     "timestamp": 1594958255483,
        //     "senderId": "3bb179af-bcc5-4fe0-9dac-c05688484649",
        //     "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);
    });

对于收到且已读的消息,需要标记为已读状态,否则会话列表里的数字始终保持原来数字,并且在下次连接GoEasy时,GoEasy会重复下发之前未标记的消息。

发送群消息


    //创建消息, 内容最长不超过3K,可以发送字符串,对象和json格式字符串
    var message = im.createTextMessage({
        text:'Hello GoEasyIM!'
    });

    var groupId = "group001";

    //发送群消息
    var promise = im.sendGroupMessage(groupId, message);

    promise.then(function () {
        console.log("Send group message successfully.");
    }).catch(function (error) {
        console.log("Failed to send 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 ""