Home  >  Article  >  Java  >  JAVA Web real-time message background server push technology---GoEasy

JAVA Web real-time message background server push technology---GoEasy

高洛峰
高洛峰Original
2016-12-07 15:39:521975browse

More and more projects require the push and reception of real-time messages. I recommend everyone to use GoEasy, which is a third-party push service platform. You can easily handle real-time push using its API!

Browser compatibility: GoEasy push supports two connection methods: websocket and polling, which can support all versions of IE6 and above, and also supports other browsers such as Firefox, Chrome, Safari, etc.

Supports different development languages: GoEasy Push provides a Restful API interface. No matter which language your background program uses, you can achieve background real-time push through the Restful API. Such as: Java, PHP, C#, Ruby, Python, C, C++, ASP.NET, Node.js...

Supports backend and frontend push: Restful API is used in the backend, and goeasy.js is used in the frontend; very simple to use!

Let me introduce the steps to use GoEasy:

1. You need to register an account on the goeasy official website and create an application. After the application is created, the system will generate two keys for it by default: publish key and subscribe key.

2. Real-time subscription and reception at the front desk

You only need to introduce goeasy.js, and then call goeasy's subscribe method to subscribe to a channel. When subscribing, you can use either publish key or subscribe key. Messages can be received in real time through the callback function of subscribe's parameter onMessage.

3. For front-end real-time push

you still need to introduce goeasy.js (if the page has already been introduced, you can no longer import it), and then call goeasy's publish method to push messages to the subscribed channels. You can only use publish when pushing. key.

4. Backend real-time push

Call GoEasy Restful API, use post method to access http://goeasy.io/goeasy/publish, and also need to bring three necessary parameters:

appkey: publish key

channel: The channel you subscribed to

content: Pushing content

is that simple.

Principle of push: The implementation principle of GoEasy is very simple, that is, one end of the push message is only responsible for pushing, and the pages that need to be received need to be subscribed in advance. What to subscribe to? Subscribe to the channel. When a message is pushed to a certain channel, the client subscribes to the same channel, thus ensuring accurate reception. Through the channel, we can specify which pages or users can receive messages pushed from this channel.

Below I will post a small example I wrote before, which uses Javascript to subscribe, push, receive, and unsubscribe on the web page. The appkey inside is the appkey of goeasy official demo.

<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 !== &#39;undefined&#39;){
    var goEasy = new GoEasy({
      appkey: &#39;ba821151-e043-4dfb-a954-c73744c8d323&#39;,
      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: &#39;notification&#39;,
        onMessage: function(message){
          console.log(&#39;Meessage received:&#39;+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: &#39;notification&#39;,
        message: &#39;You received a new notification&#39;,
        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>

The above is the content of JAVA Web real-time message background server push technology---GoEasy. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Java date conversionNext article:Java date conversion