[undertow-dev] Embedded undertow and atmosphere framework websockets

Aleksandar Vidakovic cheetah at monkeysintown.com
Tue May 13 11:01:27 EDT 2014


Hi Dragan,

... I just had a similar issue a couple of days ago... my setup is 
slightly different from yours (using Spring behind the scenes), but it 
shouldn't matter...

The problem was (in my case) the Atmosphere servlet mapping and the path 
parameter in the annotated handler service...

I had (like you) an asterisk ("*") in the servlet mapping and that lead 
to a similar problem that you are describing (messages seem to arrive on 
the server side, but broadcasts to subscribed clients don't work).

Also: Atmosphere was picky concerning the path parameter in my annotated 
handler service (see below).

So... I'd suggest you try something like this (just showing the relevant 
parts):

[java]

public class Bootstrap {
...

     .addMapping("/chat")

...
     // Not sure if this is required and related to your problem... 
including it anyway...
     final Xnio xnio = Xnio.getInstance("nio", 
Undertow.class.getClassLoader());
     final XnioWorker xnioWorker = 
xnio.createWorker(OptionMap.builder().getMap());
     final WebSocketDeploymentInfo webSockets = new 
WebSocketDeploymentInfo().setWorker(xnioWorker);

     servletBuilder.addServletContextAttribute(ATTRIBUTE_NAME, webSockets);

...

     final DeploymentManager manager = 
defaultContainer().addDeployment(servletBuilder);
     manager.deploy();

     // Not sure if it matters, but I don't use a path handler
     Undertow server = Undertow.builder()
             .addHttpListener("8080", "localhost")
             .setHandler(manager.start())
             .build();
     server.start();
}

// Not sure if you are using the annotations... including it anyway... I 
think at least the path parameter has to be defined (interceptors can 
also be defined in servlet init param)... Atmosphere didn't pick the 
handler in my case when left out
@Singleton
@ManagedService(
     path = "/{id}",
     broadcasterCache = UUIDBroadcasterCache.class,
     interceptors = {AtmosphereResourceLifecycleInterceptor.class, 
CorsInterceptor.class, TrackMessageSizeInterceptor.class, 
SuspendTrackerInterceptor.class},
     broadcaster = SimpleBroadcaster.class)
public class Chat {
...
}

[/java]

... and then on the client side:

[javascript]

var globalCallback = function (response) {
     // do something
};

var roomId = '123';

var rq = $.atmosphere.subscribe('http://localhost:8080/chat/' + roomId, 
globalCallback, $.atmosphere.request = {
     enableXDR: true,
     logLevel: 'debug',
     contentType: 'application/json',
     transport: 'websocket',
     onError: function (response) {
     },
     onClose: function (response) {
     },
     onOpen: function (response) {
     },
     onMessage: function (response) {
     },
     onReopen: function (request, response) {
     },
     onReconnect: function (request, response) {
     },
     onMessagePublished: function (response) {
     },
     onTransportFailure: function (reason, request) {
     },
     onLocalMessage: function (request) {
     },
     onFailureToReconnect: function (request, response) {
     },
     onClientTimeout: function(request){
     },
     callback: function () {
     }
});

[/javascript]

Note: I am using the latest 2.2.0-RC1 jars with Atmosphere JQuery 2.2.0

Hope it helps...

Cheers,

Aleks

On 13.05.2014 14:54, Dragan Jotanovic wrote:
> Hi,
>
> I'm struggling to make simple chat atmosphere application to work with 
> embedded undertow.
> The app seems to connect to websocket, and requests are passing 
> through to AtmosphereHandler but response never reaches client. I 
> suppose that I'm missing something in my undertow configuration but 
> don't know what. Same code works when dropped into Tomcat or Jetty.
>
> Here is my bootstrap class:
>
> public class Bootstrap {
>     public static void main(String[] args) throws Exception {
>         // deploy to undertow
>         DeploymentInfo servletBuilder = Servlets.deployment()
> .setClassLoader(Bootstrap.class.getClassLoader())
>                 .setContextPath("")
>                 .setDeploymentName("chat")
>                 .setDefaultEncoding("UTF-8")
>                 .setUrlEncoding("UTF-8")
>                 .setResourceManager(new FileResourceManager(new 
> File(""), 0))
>                 .addWelcomePage("index.html");
> servletBuilder.addServlet(Servlets.servlet("AtmosphereServlet", 
> AtmosphereServlet.class)
> .addInitParam("org.atmosphere.cpr.AtmosphereHandler", 
> "org.atmosphere.samples.chat.Chat")
>                 .addMapping("/chat/*")
>                 .setAsyncSupported(true));
>
>         final WebSocketDeploymentInfo webSocketDeploymentInfo = new 
> WebSocketDeploymentInfo();
> servletBuilder.addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, 
> webSocketDeploymentInfo);
>
>         DeploymentManager manager = 
> Servlets.defaultContainer().addDeployment(servletBuilder);
>         manager.deploy();
>
>         HttpHandler servletHandler = manager.start();
>         PathHandler path = 
> Handlers.path(Handlers.redirect("/")).addPrefixPath("/", servletHandler);
>         Undertow server = Undertow.builder()
>                 .addHttpListener(8080, "0.0.0.0")
>                 .setHandler(path)
>                 .build();
>         server.start();
>     }
> }
>
> Does anyone know how to properly configure websockets support so that 
> it works with Atmosphere Framework?
>
> Complete example can be seen here:
> https://github.com/djotanov/atmosphere-undertow
>
>
> Regards,
> Dragan
>
>
> _______________________________________________
> undertow-dev mailing list
> undertow-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/undertow-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/undertow-dev/attachments/20140513/1f8c2983/attachment-0001.html 


More information about the undertow-dev mailing list