PHP基於websocket即時通訊的實現—GoEasy

來源:互聯網
上載者:User

標籤:php後台推送

PHP websocket即時訊息推送

實現步驟如下:
1. 擷取 GoEasy appkey.
在 goeasy 官網上註冊一個帳號,並建立一個 app. APP 建立好後系統會為該 app 自動產生兩個 key, 一個既可以用來接收又可以用來推送 (supper key),另一個只可以用來接收(subscriber key).
2. 客訂閱一個 channel.
a. 在用戶端引入 goeasy.js , js 地址: http://cdn.goeasy.io/goeasy.js
需要注意的是,不能將 goeasy.js 下載到本地,因為 goeasy 會根據不同瀏覽器來提供不同的 js. 官網上也有相應說明。
b. 訂閱代碼

Js代碼  650) this.width=650;" class="star" src="http://fengziit.iteye.com/images/icon_star.png" alt="收藏代碼" />

  1. var goEasy = new GoEasy({appkey: ‘subscriber key‘});//這裡我用的是 subscriber key  

 不需要推送任何訊息,所以沒有必要使用 supper key

Js代碼  650) this.width=650;" class="star" src="http://fengziit.iteye.com/images/icon_star.png" alt="收藏代碼" />

  1. goEasy. subscribe({  

  2. channel: ‘csdnNotification‘,  

  3. onMessage: function(message){  

  4. alert(‘Meessage received:‘+message.content);//接收到推送的訊息  

  5. }  

  6. });   

 
3. 前台推送及後台推送
3.1. 前台往相同 channel 上推送
在引入了 goeasy.js 的頁面,直接調用 goEasy. publish(‘csdnNotification‘, ‘您的好友 222 已上線‘); 用法與訂閱類似。
3.2. 後台往相同 channel 上推送. 用 GoEasy 提供的 restful API 實現
API url: http://goeasy.io/goeasy/publish
方式: Post
參數列表:
channel: ‘csdnNotification‘ //與訂閱的 channel 必須一致,否則用戶端將收不到訊息。因此我們也可以根據 channel 來控制哪些用戶端可以接收。
appkey: ‘supper key‘ //這裡必須使用 supper key
content: ‘blabla …..‘ //要推送的內容


PHP基於websocket即時通訊的實現—GoEasy

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.