发送私聊消息 - GoEasy IM

前置条件

发送私聊消息


    var im = goeasy.im;
    //创建消息, 内容最长不超过3K,可以发送字符串,对象和json格式字符串
    let textMessage = im.createTextMessage({
        text:'Hello, GoEasyIM', //消息内容
        to : {
            type : GoEasy.IM_SCENE.PRIVATE,   //私聊还是群聊,群聊为GoEasy.IM_SCENE.GROUP
            id : 'user002',
            data:{"avatar":"/www/xxx.png","nickname":"Neo"} //好友扩展数据, 任意格式的字符串或者对象,用于更新会话列表conversation.data
        }
    });

    //发送消息
    im.sendMessage({
        message:textMessage,
        onSuccess: function (message) { //发送成功
          console.log("Private message sent successfully.", message);
        },
        onFailed: function (error) { //发送失败
          console.log("Failed to connect GoEasy, code:"+error.code+ ",error:"+error.content);
        }
    });

接收私聊消息

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

    var onPrivateMessageReceived = function(message) {
        //文字消息
        // {
        //     "messageId": "8f0e27a0c7e111eab347b726da4416bd",
        //     "timestamp": 1594958217087,
        //     "type": "text",
        //     "senderId": "3bb179af-bcc5-4fe0-9dac-c05688484649",
        //     "payload": {
        //         "text": "Hello, GoEasyIM"
        //     },
        //     "receiverId": "fdee46b0-4b01-4590-bdba-6586d7617f95"
        // }
        console.log("received private message:" + JSON.stringify(message));
    };
    //监听和接收单聊消息
    im.on(GoEasy.IM_EVENT.PRIVATE_MESSAGE_RECEIVED, onPrivateMessageReceived);

results matching ""

    No results matching ""