0
votes

First of all thanks Moishe for the wonderful Channel API. I am running a simple scenario given in the Google Channel API docs. The problem that I am facing is that the channel is immediately closed after its opened.

/* Client Side */
public class Feed extends HttpServlet {


private static String feed=
   "<html>" +
   "<head>" +
   "<title>Login</title>" +
   "<script type=\"text/javascript\" src="/_ah/channel/jsapi\"></script>" +
   "</head>" +
   "<body>" + 
   "Feed" +
   "<script>" +
   "channel=new goog.appengine.Channel('{{ token }}');" +
   "socket=channel.open();" +
   "socket.onOpen=alert(\"channel opened");" +
   "socket.onMessage=alert(\"New Message\");" +
   "socket.onClose=alert(\"Socket Closed\");" +
   "socket.onError=alert(\"Error\");" +
   "</script>" +
   "</body>" +
   "</html>";"

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException{

    ChannelService channelservice=ChannelServiceFactory.getChannelService();
    String token=channelservice.createChannel("sample");
    feed = feed.replaceAll("\\{\\{ token \\}\\}", token);
    res.setContentType("text/html");
    res.getWriter().write(feed);
 }

}

/* Server Side*/

public class QuestAsk extends HttpServlet{

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
    {
        ChannelService channelService=ChannelServiceFactory.getChannelService();
        channelService.sendMessage(new ChannelMessage("sample","sample message"));
    }
 }

I issue a GET request to Feed.

That is when I see channel open followed by an undefined message received->channel closed->Channel error.

Obviously, I am overlooking here something. Would very much appreciate if someone can point it out. Thanks for any help.

Best Regards

JR

1

1 Answers

1
votes

There are two problems that I see:

First, onopen etc. have no capitalization, so you are assigning the wrong values in socket. You should use socket.onopen, socket.onmessage, socket.onclose and socket.onerror.

Second, you're assigning the result of a call to alert() to socket.onOpen etc. Instead you want to assign a function to these functions, so you should do something like this:

socket.onopen = function() {alert('Channel opened.');};
// etc

I believe this will fix your code.