首页 > php教程 > PHP开发 > 正文

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

高洛峰
发布: 2016-11-04 14:50:27
原创
2354人浏览过

越来越多的项目需要用到实时消息的推送与接收,怎样用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

立即学习PHP免费学习笔记(深入)”;

 下面我介绍一下使用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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

<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>

登录后复制
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号