0

After a while I got the first part of the Google Channel API working. Now I have some problems with sending a message from the JavaScript client to the server. Here is the servlet for the Google Channel API connection:

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    if (user != null) {
        ChannelService channelService = ChannelServiceFactory.getChannelService();
        String token = channelService.createChannel(user.getUserId());

        FileReader reader = new FileReader("index.html");
        CharBuffer buffer = CharBuffer.allocate(16384);
        reader.read(buffer);
        reader.close();

        String index = new String(buffer.array());
        index = index.replaceAll("\\{\\{ token \\}\\}", token);

        resp.setContentType("text/html");
        resp.getWriter().write(index);
    } else {
        resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
    }

The index.html looks like following:

<script src="/_ah/channel/jsapi"></script>

<body>
    <script type="text/javascript">
        onOpened = function() {
            alert("opened");
        }

        var token = "{{ token }}";
        var channel = new goog.appengine.Channel(token);
        var handler = {
            'onopen' : onOpened,
            'onmessage' : onMessage,
            'onerror' : function() {
            },
            'onclose' : function() {
            }
        };
        var socket = channel.open(handler);
        socket.onopen = onOpened;
        socket.onmessage = onMessage;

        function sendMessage() {
            // Send JSON object to server   
        }
    </script>
    <h1>Google Test Channel API</h1>
    <form>
        <input type="button" value="Send" onclick="sendMessage();">
    </form>
</body>

If I load the application I get the opened alert, I believe the connection is working. Now I would like to send a message to the server, if someone clicks on the button.

I read that I have to use the XMLHttpRequest function with POST or GET. But I won’t pass a new url, I would just pass a value. Is there a way to send a JSON object to the server?

Something like:

{
    "message": "This is a JavaClient message!"
}
Andre Hofmeister
  • 2,814
  • 10
  • 43
  • 68
  • I am not even to create channel first time. Can you please answer this http://stackoverflow.com/questions/34332222/unable-to-create-new-channel-in-javascript-of-channel-api – Sunil Garg Dec 17 '15 at 10:48

3 Answers3

1

This doesn't really have much to do with the Chanel API - it's just a standard xhr call back to the server. You can use standard code like this, or use a library such as jquery like this.

If you need to parse javascript into JSON use JSON.stringify().

At the server you can process your request as required, send a response back to the client via standard HTTP or use the Chanel API to send a message not just to the original client, but to all connected clients.

Gwyn Howell
  • 5,125
  • 2
  • 28
  • 45
  • Yea, but how do I react at the server side? Is the second paramter from `xmlhttp.open` a servlet? In every example they pass a url as paramter, could it also be a JSON object? Greetz. – Andre Hofmeister Sep 28 '12 at 14:08
  • it's a link which is mapped to a servlet yeah. you then treat it as any other http request. it's pretty standard stuff i recommend you read this -> https://developers.google.com/appengine/articles/rpc. It's python, but the concepts are the same for java – Gwyn Howell Sep 28 '12 at 14:14
0

Are you familiair with the channel API blog post of Nick Johnson:

http://blog.notdot.net/2011/07/Using-the-Channel-API-on-App-Engine-for-instant-traffic-analysis

voscausa
  • 9,982
  • 2
  • 29
  • 55
0

App Engine's Channel API is pretty much only for sending messages from the server->client. It's a one way channel.

That's fine because it's not hard to build the client->server channel, but you do have to build it yourself, it's not for free. You do have to define your own url for the client->server message, then use an XMLHttpRequest to send your message to that url.

There's a small exception in that the Channel API also has a built on mechanism to notify the server of connects/disconnects. Under the cover, these are done using XMLHttpRequests to predefined urls (I think it's something like _ah/channel/disconnect or something. These won't be useful to you since they're automatically called - you can't define when they are called, or the content.

So yeah, just implement your own URL handler, and send your JSON data to that URL.

dragonx
  • 14,752
  • 24
  • 41