搜尋
首頁php教程PHP开发PHP Web即時訊息後台伺服器推播技術GoEasy

越来越多的项目需要用到实时消息的推送与接收,怎样用php实现最方便呢?我这里推荐大家使用GoEasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送!

浏览器兼容性:GoEasy推送 支持websocket 和polling两种连接方式,从而可以支持IE6及其以上的所有版本,同时还支持其它浏览器诸如Firefox, Chrome, Safari等等。

支持不同的开发语言:    GoEasy推送 提供了Restful API接口,无论你的后台程序用的是哪种语言都可以通过Restful API来实现后台实时推送。如:Java,PHP, C#, Ruby, Python, C, C++, ASP.NET,Node.js...

支持后台及前台推送: 后台用Restful API, 前台用goeasy.js; 运用十分简单!

 PHP Web实时消息后台服务器推送技术-GoEasy

 下面我介绍一下使用GoEasy的步骤:

 1. 你需要到goeasy官网上注册一个账号,并创建一个应用,应用创建好后系统会默认为它生成两个key: publish key和subscribe key

 2. 前台实时订阅及接收

    只需要引入goeasy.js,然后调用goeasy的subscribe方法订阅一个channel即可,订阅时无论是用publish key还是subscribe key都可以。通过subscribe的参数 onMessage的回调函数可以实时接收到消息。

 3. 前台实时推送

    还是需要引入goeasy.js(如果该页面已经引入了可不在引入),然后调用goeasy的publish方法向已订阅的channel上推送消息即可,推送时只能用publish key。

 4. 后台实时推送

     调用GoEasy Restful API, 用post方式访问http://goeasy.io/goeasy/publish, 同时还需要带上三个必要参数:

    appkey: publish key

    channel: 你订阅了的channel

   content: 推送内容

 就是这么简单。

下面我将之前写的一个小实例贴出来,里面用了Javascript 在web页面进行订阅,推送,接收,以及取消订阅的例子,里面的appkey用的是goeasy官方的demo 的appkey.

<html>
<head>
<title>GoEasy Test</title>

<script type="text/javascript" src="https://cdn.goeasy.io/goeasy.js"></script>
<script type="text/javascript">
    if(typeof GoEasy !== &#39;undefined&#39;){
        var goEasy = new GoEasy({
            appkey: &#39;ba821151-e043-4dfb-a954-c73744c8d323&#39;,
            userId:"222",
            username:"22",
            onConnected:function(){
                console.log("Connect to GoEasy success.");
            } ,
            onDisconnected:function(){
                console.log("Disconnect to GoEasy server.");
            } ,
            onConnectFailed:function(error){
                console.log("Connect to GoEasy failed, error code: "+ error.code+" Error message: "+ error.content);
            }
        });
    }
            
    subscribe();
    function subscribe(){
             goEasy.subscribe({
                channel: &#39;notification&#39;,
                onMessage: function(message){
                    console.log(&#39;Meessage received:&#39;+message.content);
                },
                onSuccess:function(){

                    console.log("Subscribe the Channel successfully.");

                },

                onFailed: function(error){

                    console.log("Subscribe the Channel failed, error code: "+ error.code + " error message: "+ error.content);

                }

            });

    }
            
     function publishMessage(){
          goEasy.publish({
                channel: &#39;notification&#39;,
                message: &#39;You received a new notification&#39;,
                onSuccess:function(){

                    console.log("Publish message success.");

                },
                onFailed: function(error){

                    console.log("Publish message failed, error code: "+ error.code +" Error message: "+ error.content);

                }
            });
     
     }
                  
     function unsubscribe(){
                goEasy.unsubscribe({
                    channel:"notification",
                    onSuccess: function(){

                        console.log("Cancel Subscription successfully.");

                    },
                    onFailed: function(error){

                        console.log("Cancel the subscrition failed, error code: "+ error.code + "error message: "+ error.content);
                    }

                });
            }
        
 </script>
</head>
<body>
  <input type="button" value="publish" onclick="publishMessage()"/>
  <input type="button" value="unsubscribe" onclick="unsubscribe()"/>
  <input type="button" value="subscribe" onclick="subscribe()"/>
</body>
</html>


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用