JAVA Web即時訊息後台伺服器推送技術---GoEasy_javascript技巧

來源:互聯網
上載者:User

越來越多的項目需要用到即時訊息的推送與接收,我這裡推薦大家使用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; 運用十分簡單!

下面我介紹一下使用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: 推送內容

 就是這麼簡單。

推送的原理:GoEasy的實現原理很簡單,就是推送訊息的一端只負責推送,而需要接收的頁面需要預先訂閱。訂閱什麼呢?訂閱channel。往 某個channel上推送訊息,用戶端就訂閱相同的channel,這樣就可以確保準確接收。通過channel我們可以自己指定哪些頁面或哪些使用者可以 接收到從這個channel上推送出來的訊息。

下面我將之前寫的一個小執行個體貼出來,裡面用了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 !== '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>

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.