Google App Engine Channel API

后端 未结 2 1029
北海茫月
北海茫月 2021-02-03 13:40

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

I went through Channel API Overview (Java) but the code posted there w

2条回答
  •  名媛妹妹
    2021-02-03 14:27

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

    It lays out everything from creating a Channel(Client & Server), Sending a message on a channel (client & server) as well as a simple framework Java clients can utilize to interact with Channels. I too had a hard time understanding Google's Documentation and making sense of it all. I hope this information is still relavent and helpful :-)

    The Complete Source Code and Chat Example can be found on GitHub: https://github.com/gvsumasl/jacc

    Here's some sample code, I hope this helps :-)


    Java Client Side Channel Creation (Using ChannelAPI Framework: Jacc)

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

    Java Server Side Channel Creation:

    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());
      }
    }
    

    Java Client Message Sending (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");
        }
    }
    

    Java Server Side Message Sending:

    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));
      }
    }
    

提交回复
热议问题