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

前端之家收集整理的这篇文章主要介绍了JAVA Web实时消息后台服务器推送技术---GoEasy前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

越来越多的项目需要用到实时消息的推送与接收,我这里推荐大家使用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方式访问, 同时还需要带上三个必要参数:

就是这么简单。

推送的原理:GoEasy的实现原理很简单,就是推送消息的一端只负责推送,而需要接收的页面需要预先订阅订阅什么呢?订阅channel。往 某个channel上推送消息,客户端就订阅相同的channel,这样就可以确保准确接收。通过channel我们可以自己指定哪些页面或哪些用户可以 接收到从这个channel上推送出来的消息。

下面我将之前写的一个小实例贴出来,里面用了Javascript 在web页面进行订阅,推送,接收,以及取消订阅的例子,里面的appkey用的是goeasy官方的demo 的appkey.

GoEasy Test

<script type="text/javascript" src="https://cdn.goeasy.io/goeasy.js"&gt;
<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.");

    },on<a href="https://www.jb51.cc/tag/Failed/" target="_blank" class="keywords">Failed</a>: function(error){

      console.log("Subscribe the Channel <a href="https://www.jb51.cc/tag/Failed/" target="_blank" class="keywords">Failed</a>,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.");

    },on<a href="https://www.jb51.cc/tag/Failed/" target="_blank" class="keywords">Failed</a>: function(error){

      console.log("Publish message <a href="https://www.jb51.cc/tag/Failed/" target="_blank" class="keywords">Failed</a>,error code: "+ error.code +" Error message: "+ error.content);

    }
  });

}

function unsubscribe(){
goEasy.unsubscribe({
channel:"notification",onSuccess: function(){

        console.log("Cancel Subscription successfully.");

      },on<a href="https://www.jb51.cc/tag/Failed/" target="_blank" class="keywords">Failed</a>: function(error){

        console.log("Cancel the subscrition <a href="https://www.jb51.cc/tag/Failed/" target="_blank" class="keywords">Failed</a>,error code: "+ error.code + "error message: "+ error.content);
      }

    });
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

原文链接:https://www.f2er.com/js/44629.html

猜你在找的JavaScript相关文章