GoEasyIM - 群聊

初始化


    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示例
        // {
        //     "timestamp": 1592807014215,
        //     "senderId": "user001",
        //     "groupId": "group001",
        //     "payload": "大家好,新来的,请大家多关照"
        // }
        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);
    });

发送群消息


    //创建消息, 内容最长不超过3K,可以发送字符串,对象和json格式字符串
    var message = im.createMessage("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 ""