越來越多的項目需要用到實時消息的推送與接收,怎樣用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.
1 <html>
2 <head>
3 <title>GoEasy Test</title>
4
5 <script type="text/javascript" src="https://cdn.goeasy.io/goeasy.js"></script>
6 <script type="text/javascript">
7 if(typeof GoEasy !== 'undefined'){
8 var goEasy = new GoEasy({
9 appkey: 'ba821151-e043-4dfb-a954-c73744c8d323',
10 userId:"222",
11 username:"22",
12 onConnected:function(){
13 console.log("Connect to GoEasy success.");
14 } ,
15 onDisconnected:function(){
16 console.log("Disconnect to GoEasy server.");
17 } ,
18 onConnectFailed:function(error){
19 console.log("Connect to GoEasy failed, error code: "+ error.code+" Error message: "+ error.content);
20 }
21 });
22 }
23
24 subscribe();
25 function subscribe(){
26 goEasy.subscribe({
27 channel: 'notification',
28 onMessage: function(message){
29 console.log('Meessage received:'+message.content);
30 },
31 onSuccess:function(){
32
33 console.log("Subscribe the Channel successfully.");
34
35 },
36
37 onFailed: function(error){
38
39 console.log("Subscribe the Channel failed, error code: "+ error.code + " error message: "+ error.content);
40
41 }
42
43 });
44
45 }
46
47 function publishMessage(){
48 goEasy.publish({
49 channel: 'notification',
50 message: 'You received a new notification',
51 onSuccess:function(){
52
53 console.log("Publish message success.");
54
55 },
56 onFailed: function(error){
57
58 console.log("Publish message failed, error code: "+ error.code +" Error message: "+ error.content);
59
60 }
61 });
62
63 }
64
65 function unsubscribe(){
66 goEasy.unsubscribe({
67 channel:"notification",
68 onSuccess: function(){
69
70 console.log("Cancel Subscription successfully.");
71
72 },
73 onFailed: function(error){
74
75 console.log("Cancel the subscrition failed, error code: "+ error.code + "error message: "+ error.content);
76 }
77
78 });
79 }
80
81 </script>
82 </head>
83 <body>
84 <input type="button" value="publish" onclick="publishMessage()"/>
85 <input type="button" value="unsubscribe" onclick="unsubscribe()"/>
86 <input type="button" value="subscribe" onclick="subscribe()"/>
87 </body>
88 </html>