欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JAVA Web實(shí)時(shí)消息后臺(tái)服務(wù)器推送技術(shù)---GoEasy

 更新時(shí)間:2016年11月04日 15:41:31   作者:EmmaGong  
本篇文章主要介紹了PHP實(shí)現(xiàn)Web實(shí)時(shí)消息后臺(tái)服務(wù)器推送技術(shù),這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。

越來(lái)越多的項(xiàng)目需要用到實(shí)時(shí)消息的推送與接收,我這里推薦大家使用GoEasy, 它是一款第三方推送服務(wù)平臺(tái),使用它的API可以輕松搞定實(shí)時(shí)推送!

瀏覽器兼容性:GoEasy推送 支持websocket 和polling兩種連接方式,從而可以支持IE6及其以上的所有版本,同時(shí)還支持其它瀏覽器諸如Firefox, Chrome, Safari等等。

支持不同的開(kāi)發(fā)語(yǔ)言:GoEasy推送 提供了Restful API接口,無(wú)論你的后臺(tái)程序用的是哪種語(yǔ)言都可以通過(guò)Restful API來(lái)實(shí)現(xiàn)后臺(tái)實(shí)時(shí)推送。如:Java,PHP, C#, Ruby, Python, C, C++, ASP.NET,Node.js...

支持后臺(tái)及前臺(tái)推送: 后臺(tái)用Restful API, 前臺(tái)用goeasy.js; 運(yùn)用十分簡(jiǎn)單!

下面我介紹一下使用GoEasy的步驟:

 1. 你需要到goeasy官網(wǎng)上注冊(cè)一個(gè)賬號(hào),并創(chuàng)建一個(gè)應(yīng)用,應(yīng)用創(chuàng)建好后系統(tǒng)會(huì)默認(rèn)為它生成兩個(gè)key: publish key和subscribe key

 2. 前臺(tái)實(shí)時(shí)訂閱及接收

只需要引入goeasy.js,然后調(diào)用goeasy的subscribe方法訂閱一個(gè)channel即可,訂閱時(shí)無(wú)論是用publish key還是subscribe key都可以。通過(guò)subscribe的參數(shù) onMessage的回調(diào)函數(shù)可以實(shí)時(shí)接收到消息。

 3. 前臺(tái)實(shí)時(shí)推送

還是需要引入goeasy.js(如果該頁(yè)面已經(jīng)引入了可不在引入),然后調(diào)用goeasy的publish方法向已訂閱的channel上推送消息即可,推送時(shí)只能用publish key。

 4. 后臺(tái)實(shí)時(shí)推送

調(diào)用GoEasy Restful API, 用post方式訪問(wèn)http://goeasy.io/goeasy/publish, 同時(shí)還需要帶上三個(gè)必要參數(shù):

  • appkey: publish key
  • channel: 你訂閱了的channel
  • content: 推送內(nèi)容

 就是這么簡(jiǎn)單。

推送的原理:GoEasy的實(shí)現(xiàn)原理很簡(jiǎn)單,就是推送消息的一端只負(fù)責(zé)推送,而需要接收的頁(yè)面需要預(yù)先訂閱。訂閱什么呢?訂閱channel。往 某個(gè)channel上推送消息,客戶端就訂閱相同的channel,這樣就可以確保準(zhǔn)確接收。通過(guò)channel我們可以自己指定哪些頁(yè)面或哪些用戶可以 接收到從這個(gè)channel上推送出來(lái)的消息。

下面我將之前寫的一個(gè)小實(shí)例貼出來(lái),里面用了Javascript 在web頁(yè)面進(jìn)行訂閱,推送,接收,以及取消訂閱的例子,里面的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 !== 'undefined'){
    var goEasy = new GoEasy({
      appkey: 'ba821151-e043-4dfb-a954-c73744c8d323',
      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: 'notification',
        onMessage: function(message){
          console.log('Meessage received:'+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: 'notification',
        message: 'You received a new notification',
        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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論