GoEasyIM - 一对一单聊

初始化


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

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

    var user = {
        id:'user001'
    }

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

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

接收私聊消息

    var onPrivateMessageReceived = function(message) {
        //单聊接收message示例
        // {
        //     "timestamp": 1592807228506,
        //     "senderId": "user001",
        //     "payload": "来  嗨起走",
        //     "receiverId": "user002"
        // }
        console.log("收到消息:senderId:" + message.senderId +", content:" + message.payload);
    };
    //监听和接收单聊消息
    im.on(GoEasyIM.EVENT.PRIVATE_MESSAGE_RECEIVED, onPrivateMessageReceived);

发送消息


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

    //发送单聊消息
    var promise = im.sendPrivateMessage(userId, message);

    promise.then(function() {
       console.log("Private message sent successfully.");
    }).catch(function(error) {
        console.log("Failed to send private message,code:" + error.code +",error"+error.content);
    });

results matching ""

    No results matching ""