Websocket用户离线消息 - GoEasy PubSub

  • 本特性为高级功能,默认不开通,付费应用,可以在我的应用->查看详情,高级功能里自助开通。

接收用户离线消息

当一个用户离线,再重新在线后,可以接收到该用户离线期间的所有消息。
离线:一个用户,订阅了某个channel的所有客户端被关闭
重新在线:当用户打开新的客户端,并重新订阅了某个channel

如何启用用户离线消息补发

很简单,只需要在连接GoEasy时,传入一个该用户的唯一标识符(用户的ID,uuid或其它唯一标识),该用户就可以在线后,接收到离线期间的消息。


    var goeasy = GoEasy.getInstance({
        host:"hangzhou.goeasy.io",  //若是新加坡区域:singapore.goeasy.io
        appkey:"您的common key",
        modules:['pubsub']//根据需要,传入‘pubsub’或'im’,或数组方式同时传入
    });

    goeasy.connect({
        id: "客户唯一标识,如 user-001",
        onSuccess: function () {  //连接成功
            console.log("GoEasy connect successfully.") //连接成功
        },
        onFailed: function (error) { //连接失败
            console.log("Failed to connect GoEasy, code:"+error.code+ ",error:"+error.content);
        },
        onProgress:function(attempts) { //连接或自动重连中
            console.log("GoEasy is connecting", attempts);    
        }
    });

示例

准备两个html页面

接收端 goeasy_receive.html

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <title>GoEasy receive</title>
        <script type="text/javascript" src="http://cdn.goeasy.io/goeasy-2.1.11.min.js"></script>
        <script type="text/javascript">

            var goeasy = GoEasy.getInstance({
                 host:"hangzhou.goeasy.io",  //若是新加坡区域:singapore.goeasy.io
                 appkey:"您的common key",
                 modules:['pubsub']//根据需要,传入‘pubsub’或'im’,或数组方式同时传入
            });

            goeasy.connect({
                id: "用户唯一标识,如 user-001",
                onSuccess: function () {  //连接成功
                    console.log("GoEasy connect successfully.") //连接成功
                },
                onFailed: function (error) { //连接失败
                    console.log("Failed to connect GoEasy, code:"+error.code+ ",error:"+error.content);
                },
                onProgress:function(attempts) { //连接或自动重连中
                    console.log("GoEasy is connecting", attempts);    
                }
            });            

            goeasy.pubsub.subscribe({
                channel: "my_channel",
                onSuccess: function () {
                    console.log("订阅成功");
                },
                onFailed: function (error) {
                    console.log("订阅失败: "+error.content);
                },onMessage: function (message) {
                    console.log("Channel:" + message.channel + " content:" + message.content);
                }
            });
        </script>
    </head>
    <body>
        <div id="tip">请打开浏览器控制台,查看输出内容</div>
    </body>
    </html>

发送端goeasy_publish.html

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <title>GoEasy publish</title>
        <script type="text/javascript" src="http://cdn.goeasy.io/goeasy-2.1.11.min.js"></script>
        <script type="text/javascript">

            var goeasy = GoEasy.getInstance({
                host:"hangzhou.goeasy.io",  //若是新加坡区域:singapore.goeasy.io
                appkey:"您的common key",
                modules:['pubsub']//根据需要,传入‘pubsub’或'im’,或数组方式同时传入
            });

            goeasy.connect({
                id: "用户唯一标识,如 user-001",
                onSuccess: function () {  //连接成功
                    console.log("GoEasy connect successfully.") //连接成功
                },
                onFailed: function (error) { //连接失败
                    console.log("Failed to connect GoEasy, code:"+error.code+ ",error:"+error.content);
                },
                onProgress:function(attempts) { //连接或自动重连中
                    console.log("GoEasy is connecting", attempts);    
                }
            });  

            function publish() {
                var message = document.getElementById("messageContent").value;
                goeasy.pubsub.publish({
                    channel: "my_channel",
                    message: message,
                    onSuccess: function () {
                        console.log("发送成功");
                    },
                    onFailed: function (error) {
                        console.log("发送失败: "+error.content);
                    }
                });
            }
        </script>
    </head>
    <body>
        <textarea id="messageContent" rows="5" cols="50"></textarea>
        <button type="button" onclick="publish();">发送</button>
    </body>
    </html>

操作步骤

  1. 将两个页面里需要补充的地方,补充完整: host, appkey, userId
  2. 打开goeasy_receive.html
  3. 打开goeasy_publish.html,然后点击发送按钮,然后去看goeasy_receive.html页面收到的消息
  4. 关闭goeasy_receive.html,使其处于离线状态,然后在goeasy_publish.html页面继续发送几条消息
  5. 重新打开goeasy_receive.html页面,用户在线,该用户就会收到之前离线期间的离线消息

results matching ""

    No results matching ""