his / gwt-gae-channel

Automatically exported from code.google.com/p/gwt-gae-channel
0 stars 0 forks source link

New features for the Socket #6

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
As I have been looking through the javascript source code for the clientside I 
have seen that the Socket has some more methods than this java class has.

- I think that at least the close function should be integrated. Because that's 
the way you close the open socket so you won't get any messages.

- There is also a send function which sends strings to /_ah/channel/receive . 
You can define a Servlet for this URL and so you can receive messages in the 
onPost method of the servlet.

- At the end there is a property called readyState which is an int between 0-3 
(CONNECTING, OPEN, CLOSING, CLOSED) so you can see what is the current state of 
the connection.

With this patch you can access all of these methods/property.

Original issue reported on code.google.com by buchholz...@googlemail.com on 3 Feb 2011 at 4:04

Attachments:

GoogleCodeExporter commented 9 years ago
I can add close(), but I don't see any documentation for the send() or 
readyState features at 
http://code.google.com/appengine/docs/java/channel/javascript.html

Particularly, I don't believe send() is supported through the Channel API, 
messages from client to server must be implemented as regular XHR requests. 
Even if this functionality is included in the JS source, if it's not supported 
by the App Engine server, then there's no point in exposing it in the GWT 
wrapper.

So I'll add close() in a change soon, 
http://code.google.com/p/gwt-gae-channel/issues/detail?id=1 also mentions the 
need to expose the onerror and onclose methods, which I can add as well.

Original comment by jasonhall@google.com on 3 Feb 2011 at 4:13

GoogleCodeExporter commented 9 years ago
I know that this is not documented. I just wanted to tell you that it exists 
and it is working :)

I have allready tested the send method and it works perfectly.
Why else would there be a getChannelService().parseMessage(request) function?

If you want to try it out do the following:

1. Add a servlet mapping in your web.xml
  <servlet>
    <servlet-name>channelServlet</servlet-name>
    <servlet-class>com.google.gwt.channel.server.ChannelServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>channelServlet</servlet-name>
    <url-pattern>/_ah/channel/receive</url-pattern>
  </servlet-mapping>

2. Create the class com.google.gwt.channel.server.ChannelServlet like this:

public class ChannelServlet extends HttpServlet  {
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
     ChannelMessage msg = getChannelService().parseMessage(req);
     msg.getClientId(); // ClientId
     msg.getMessage(); // Message
  }
  private static ChannelService getChannelService() {
    return ChannelServiceFactory.getChannelService();
  }
}

3. In your gwt code just do a socket.send("Hello World"); and the message gets 
send.

Original comment by buchholz...@googlemail.com on 3 Feb 2011 at 4:28

GoogleCodeExporter commented 9 years ago
Wow, that's great, I didn't realize that actually worked. Still, I'd rather 
keep the GWT wrapper limited to just methods that are documented. The Channel 
API may stop supporting this at some point, or may change things about it in 
breaking ways, and they may have decided not to document it in order to reserve 
the right. 

Still, thanks for pointing this out, let's hope they officially add this in a 
future release.

If you feel comfortable using this functionality yourself, it should be 
possible subclass the Channel class to add the send() method as a JSNI method. 
When you get your channel from ChannelFactory you can call MyChannel my = 
channel.cast(); to cast the standard Channel to your own MyChannel.

Original comment by jasonhall@google.com on 3 Feb 2011 at 4:38

GoogleCodeExporter commented 9 years ago

Original comment by jasonhall@google.com on 3 Feb 2011 at 4:39

GoogleCodeExporter commented 9 years ago
Thanks!
I already have my own implementation running but thank you for the subclassing 
tip!

If you want to know where I found these things look at this gist:
https://gist.github.com/809830
which is the readable copy of the javascript code on the appengine dev server. 
(http://127.0.0.1:8888/_ah/channel/jsapi or 
http://talkgadget.google.com/talkgadget/channel.js for the production server)

On line 5556 is the send method.
On line 5566 is the close method.
On line 5496 is the ReadyState constant defined.

All other thing are also around at these lines.

Original comment by buchholz...@googlemail.com on 3 Feb 2011 at 5:48

GoogleCodeExporter commented 9 years ago
Just another note:
These APIs are currently in the dev version of the html5 websocket API 
http://dev.w3.org/html5/websockets/
And the appengine channel is just an early implemention of it.

Original comment by buchholz...@googlemail.com on 3 Feb 2011 at 7:41

GoogleCodeExporter commented 9 years ago
The send method is *NOT* supported on the *PRODUCTION* server.
You can call it but it just returns false...

Original comment by buchholz...@googlemail.com on 2 Mar 2011 at 6:03