integrate with gwt?

3 messages Options
Embed this post
Permalink
asianCoolz

integrate with gwt?

Reply Threaded More More options
Print post
Permalink
anyone already tried integrate with gwt ? can share your experiences and maybe guides/tutorials?
Jeanfrancois Arcand

Re: integrate with gwt?

Reply Threaded More More options
Print post
Permalink
Saltu,

asianCoolz wrote:
> anyone already tried integrate with gwt ? can share your experiences and
> maybe guides/tutorials?

It should be simple to run on top of GWT. I haven't try but let me share
how this can be achieved (I will try to write a sample next week).
Mainly, you will first need to define your web.xml like:

>      <description>Atmosphere GWT</description>
>     <display-name>Atmosphere GWT</display-name>
>     <servlet>
>         <description>AtmosphereServlet</description>
>         <servlet-name>AtmosphereServlet</servlet-name>
>         <servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
>         <load-on-startup>0</load-on-startup>
>     </servlet>
>     <servlet-mapping>
>         <servlet-name>AtmosphereServlet</servlet-name>
>         <url-pattern>/*</url-pattern>
>     </servlet-mapping>

Next, you will add define your GWT Servlet inside your atmosphere.xml:

> <atmosphere-handlers>
>     <atmosphere-handler context-root="/resources"
>             class-name="org.atmosphere.handler.ReflectorServletProcessor"
>             broadcaster="org.atmosphere.cpr.DefaultBroadcaster">
>         <property name="servletClass" value="foo.bar.yourGWTServlet"/>
>     </atmosphere-handler>
> </atmosphere-handlers>

Inside your GWT Servlet (which extends
com.google.gwt.user.server.rpc.RemoteServiceServlet)

You will get the AtmosphereEvent:

    * http://is.gd/2E6kp

By doing:

   AtmosphereEvent e =
HttpServletRequest.getAttribute("org.atmosphere.cpr.AtmosphereEvent");

Then you can invoke e.suspend(), e.resume() or
e.getBroadcaster().broadcast(...) as needed.

Hope that help.

-- Jeanfrancois

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Jeanfrancois Arcand

GWT Comet sample using Atmosphere (was -> Re: integrate with gwt?)

Reply Threaded More More options
Print post
Permalink
Salut,

I've just wrote a demo hacking the code described in that blog (I'm far
from a GWT expert):

   * http://is.gd/2Kk7w

It pretty old code but it was quite simple for me to change the demo and
  make it works on top of Atmosphere. The code is a little bit hacked I
admit but it is pretty simple to understand. The sample (an Auction
Demo) defines two Servlet, one for suspending connection:

   * http://is.gd/2Klxo

and one for broadcasting bids, invoked by GWT when events happens on the
screen:

   * http://is.gd/2Kls2

First, you need to do is to defines them inside your atmosphere.xml:

> <atmosphere-handlers>
>     <atmosphere-handler context-root="/streamingService"
>         class-name="org.atmosphere.handler.ReflectorServletProcessor">
>         <property name="servletClass" value="org.gwtcomet.server.StreamingServiceImpl"/>
>     </atmosphere-handler>
>     <atmosphere-handler context-root="/streamingServlet"
>         class-name="org.atmosphere.handler.ReflectorServletProcessor">
>         <property name="servletClass" value="org.gwtcomet.server.StreamingServlet"/>
>     </atmosphere-handler>
> </atmosphere-handlers>

using the ReflectorServletProcessor (which will run those Servlets). The
web.xml looks like:


>     <description>Atmosphere GWT Auction</description>
>     <display-name>Atmosphere Auction</display-name>
>     <servlet>
>         <description>AtmosphereServlet</description>
>         <servlet-name>AtmosphereServlet</servlet-name>
>         <servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
>         <!-- Uncomment if you want to use Servlet 3.0 Async Support
>         <async-supported>true</async-supported>
>         -->
>         <load-on-startup>0</load-on-startup>
>     </servlet>
>     <servlet-mapping>
>         <servlet-name>AtmosphereServlet</servlet-name>
>         <url-pattern>/streamingServlet</url-pattern>
>     </servlet-mapping>
>     <servlet-mapping>
>         <servlet-name>AtmosphereServlet</servlet-name>
>         <url-pattern>/streamingService</url-pattern>
>     </servlet-mapping>

Mainly, we are mapping the AtmosphereServlet in order to let it dispatch
later to it's AtmosphereHandler (the Servlets)

In the Servlet that does the suspend, we just need to do:

>  95         AtmosphereEvent e =
>  96                 (AtmosphereEvent) request.getAttribute("org.atmosphere.cpr.AtmosphereEvent");
>  97
>  98         if (!e.getBroadcaster().getName().equals("GWT")){
>  99             synchronized(e.getBroadcaster()){
> 100                 e.getBroadcaster().setName("GWT");
> 101                 e.getBroadcaster().getBroadcasterConfig().addFilter(new GWTBroadcasterFilter());
> 102             }
> 103         }
> 104
> 105         e.suspend();

Just getting the AtmosphereEvent, name our Broadcaster 'GWT' and suspend
the connection. I've defined a BroascastFilter that does:

> 119     private final class GWTBroadcasterFilter implements BroadcastFilter {
> 120
> 121         public Object filter(Object message) {
> 122             try {
> 123                 StreamingServiceBusiness.Event event = (StreamingServiceBusiness.Event) message;
> 124                 StringBuffer stream = new StringBuffer();
> 125                 stream.append("<script type='text/javascript'>\n");
> 126                 stream.append("\twindow.parent.callback('" + event.queueName +
> 127                         "',unescape('" + URLEncoder.encode(event.message, "iso-8859-1")
> 128                         .replaceAll("\\x2B", "%20") + "'));\n");
> 129                 stream.append("</script>\n");
> 130                 return stream;
> 131             } catch (UnsupportedEncodingException ex) {
> 132                 Logger.getLogger(StreamingServlet.class.getName()).log(Level.SEVERE, null, ex);
> 133             }
> 134             return message;

Transform an Object into a StringBuffer (that was the original code). In
the second Servlet (the one that will be invoked by GWT), I just
retrieve the Broadcaster and do:

>
>     public void sendMessage(String queueName, String message) {
>         try {
>             bl.lookup("GWT").broadcast(new StreamingServiceBusiness.Event(queueName, message));
>         } catch (OutOfScopeException ex) {
>             Logger.getLogger(StreamingServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
>         }
>     }

That's all :-) You can download the complete sample & code from here
(once uploaded under gwt-auction)

   * http://is.gd/2KlMq

or checkout the workspace.

Let me know how it goes.

-- Jeanfrancois


Jeanfrancois Arcand wrote:

> Saltu,
>
> asianCoolz wrote:
>> anyone already tried integrate with gwt ? can share your experiences and
>> maybe guides/tutorials?
>
> It should be simple to run on top of GWT. I haven't try but let me share
> how this can be achieved (I will try to write a sample next week).
> Mainly, you will first need to define your web.xml like:
>
>>      <description>Atmosphere GWT</description>
>>     <display-name>Atmosphere GWT</display-name>
>>     <servlet>
>>         <description>AtmosphereServlet</description>
>>         <servlet-name>AtmosphereServlet</servlet-name>
>>        
>> <servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
>>         <load-on-startup>0</load-on-startup>
>>     </servlet>
>>     <servlet-mapping>
>>         <servlet-name>AtmosphereServlet</servlet-name>
>>         <url-pattern>/*</url-pattern>
>>     </servlet-mapping>
>
> Next, you will add define your GWT Servlet inside your atmosphere.xml:
>
>> <atmosphere-handlers>
>>     <atmosphere-handler context-root="/resources"            
>> class-name="org.atmosphere.handler.ReflectorServletProcessor"
>>             broadcaster="org.atmosphere.cpr.DefaultBroadcaster">
>>         <property name="servletClass" value="foo.bar.yourGWTServlet"/>
>>     </atmosphere-handler>
>> </atmosphere-handlers>
>
> Inside your GWT Servlet (which extends
> com.google.gwt.user.server.rpc.RemoteServiceServlet)
>
> You will get the AtmosphereEvent:
>
>    * http://is.gd/2E6kp
>
> By doing:
>
>   AtmosphereEvent e =
> HttpServletRequest.getAttribute("org.atmosphere.cpr.AtmosphereEvent");
>
> Then you can invoke e.suspend(), e.resume() or
> e.getBroadcaster().broadcast(...) as needed.
>
> Hope that help.
>
> -- Jeanfrancois
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]