Google App Engine Channel API

I am trying to learn the GAE channel API (using Java), but cannot figure out where to start.

I looked at the Channel API Overview (Java) , but the code that was there was incomplete for brevity.

And since I'm a beginner, it would be very helpful if a complete code example was available.

Thanks Shrey

+7
source share
2 answers

The code in the channel API overview you are attached to is pretty complete, it's a little bit everywhere. I admit that, having understood this, I feel that it is much simpler than the way they seemed to be, but I am glad that they were mistaken in providing too much information.

It is a little difficult to give a complete solution for this without extraneous information, since some of how you will use the channel API is slightly dependent on your existing application infrastructure. For this reason, I tried to understand a little what the AppEngine docs provide, so I hope you can better understand. And comments will allow you to ask specific questions if you have any.

First, a little vocabulary:

  • Channel Message: The message you want to send to clients (and probably the reason you use the channel API in the first place).
  • Channel Key: A string that is unique to the user and the area in which the user is trying to send a message.
  • Channel Icon: A string unique to any client. 1 channel token per client in 2 hours.
  • Channel Service: An AppEngine server-side class that provides tools for creating channels and sending messages through them.

On the server, you need to do the following:

ChannelService channelService = ChannelServiceFactory.getChannelService(); // The channelKey can be generated in any way that you want, as long as it remains // unique to the user. String channelKey = "xyz"; String token = channelService.createChannel(channelKey); 

Once you have the token, you just need to somehow get it on the client code. The AppEngine document you linked to does this by serving the HTML from the Java servlet and calling index.replaceAll("\\{\\{ token \\}\\}", token) .

How it works is what they did is put the string literal {{ token }} in their JavaScript code (as you will see below), so wherever {{ token }} appears in the JavaScript code, it will be replaced by the actual token generated by calling channelService.createChannel(...) above. Please note that you do not need to enter the token in the client code that you serve in this way, but this is a good place to start, as that is how they did it (and documented it).


Now that you have entered the token in JavaScript, you need to get the code with the channel marker to the client . (Please note that, as indicated above, you can also only get the token to the client and create a channel in this way). The code you have is:

 <body> <script> channel = new goog.appengine.Channel('{{ token }}'); socket = channel.open(); socket.onopen = onOpened; socket.onmessage = onMessage; socket.onerror = onError; socket.onclose = onClose; </script> </body> 

They cut out details on how to read this from a file on the server, but again, you can do it in any way that suits you. You can also just literally print the String using resp.getWriter().print(index) in your JavaServlet, where index is a String that stores the HTML / JavaScript content above. As I said at the beginning, a lot depends on what works best for your infrastructure.

They intend to define your own JavaScript functions onOpened , onMessage , onError and onClose to call when you open channels, receive a message, detect an error or close accordingly, you can simply create naive implementations to better understand what is happening:

 function onOpened() { alert("Channel opened!"); } function onMessage(msg) { alert(msg.data); } function onError(err) { alert(err); } function onClose() { alert("Channel closed!"); } 

I still recommend dividing them into separate functions so that you can more easily deploy them to play and figure it out. For more information on the JavaScript API, see the Channel JavaScript API Reference .


You need to establish a mechanism to receive the data that you want to send from the client to the server . Once again, how you want to do this does not matter. AppEngine documentation suggests setting up XMLHttpRequest for this purpose.

 sendMessage = function(path, opt_param) { path += '?g=' + state.game_key; if (opt_param) { path += '&' + opt_param; } var xhr = new XMLHttpRequest(); xhr.open('POST', path, true); xhr.send(); }; 

Here opt_param is just a string of optional parameters in the format x=1&y=2&z=3 . This is the entire infrastructure that they built for their sample Tic-Tac-Toe application, and is not critical to the functionality of the Channel API; as I said, you can make this call as you wish.

path is the path to your servlet (which you need to configure in the web.xml file), which should handle sending and receiving messages (see the next section).


After you send a message from the client to the server, you will need a servlet that can send the update to all clients with the same channel key .

 ChannelService channelService = ChannelServiceFactory.getChannelService(); // This channelKey needs to be the same as the one in the first section above. String channelKey = "xyz" // This is what actually sends the message. channelService.sendMessage(new ChannelMessage(channelKey, "Hello World!")); 

The above call to channelService.sendMessage(...) is what actually sends the message, so that it can be received by the onMessage function that you defined in the previous section.


I hope this answer is complete (and correct in this regard) to help you get started. Most of what they put into the documents (and my code here) can be copied and pasted with slight modifications.

+32
source

I am new to StackOverflow and not sure if this question is still open, but if you are still looking for a complete Java example using the Google Channel APIs, both ServerSide (Java) and Client (Java), you can find a detailed description I wrote here: http://masl.cis.gvsu.edu/2012/01/31/java-client-for-appengine-channels/

It describes everything: from creating a channel (client and server), sending a message to a channel (client and server), as well as a simple environment that Java clients can use to interact with channels. It was also difficult for me to understand the Google documentation and understand all this. I hope this information is still unchanged and useful :-)

The full source code and chat example can be found on GitHub: https://github.com/gvsumasl/jacc

Here is a sample code, hope this helps :-)


Creating a channel on the Java client side (using ChannelAPI Framework: Jacc)

 ChatListener chatListener = new ChatListener(); ChannelAPI channel = new ChannelAPI("http://localhost:8888", "key", chatListener); channel.open(); 

Creating a channel on the server side of Java:

 public class ChatChannelServlet extends HttpServlet { @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { String channelKey = req.getParameter("c"); //Create a Channel using the 'channelKey' we received from the client ChannelService channelService = ChannelServiceFactory.getChannelService(); String token = channelService.createChannel(channelKey); //Send the client the 'token' + the 'channelKey' this way the client can start using the new channel resp.setContentType("text/html"); StringBuffer sb = new StringBuffer(); sb.append("{ \"channelKey\":\"" + channelKey + "\",\"token\":\"" + token + "\"}"); resp.getWriter().write(sb.toString()); } } 

Sending Java client messages (using ChannelAPI Framework: Jacc)

 /*** * Sends your message on the open channel * @param message */ public void sendMessage(String message){ try {        channel.send(message, "/chat");    } catch (IOException e) {        System.out.println("Problem Sending the Message");    } } 

Sending a message on the Java server side:

 public class ChatServlet extends HttpServlet { @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { String channelKey = req.getParameter("channelKey"); String message = req.getParameter("message"); //Send a message based on the 'channelKey' any channel with this key will receive the message ChannelService channelService = ChannelServiceFactory.getChannelService(); channelService.sendMessage(new ChannelMessage(channelKey, message)); } } 
+13
source

All Articles