Skip to content

Using the build in WebSocket Protocol

jfarcand edited this page Sep 14, 2012 · 3 revisions

When using WebSockets, you can enable a really simple WebSocket Sub-Protocol to pass, along with a WebSocket messages, some URI which are used by Atmosphere to dispatch to the appropriate resource. The first steps is to enable the sub-protocol on the SimpleHttpProtocol:

    <init-param>
      <param-name>org.atmosphere.websocket.pathDelimiter</param-name>
      <param-value>@@</param-value>
    </init-param>

The path can be any character and that will be used to find the websocket message. On the client side, you enable it by doing:

    var request = { url: document.location.toString() + 'chat',
                    webSocketUrl : '/a URI relative to the url value',
                    webSocketPathDelimiter : "@@" }
};

For example,

    var request = { url: document.location.toString() + 'chat',
                    webSocketUrl : '/room1',
                    webSocketPathDelimiter : "@@" }

Doing:

   var subSock = $.atmosphere.subscribe(request);
   subSock.push("hello room1");

will match the @Post method of:

@Path("/chat")
 public class ResourceChat {

    @Suspend(contentType = "application/json")
    @GET
    public String suspend() {
       return "";
    }

     @Broadcast(writeEntity = false)
     @POST
     @Path("/room1")
     @Produces("application/json")
     public Response broadcast(Message message) {
          return new Response(message.author, message.message);
     }
 }

Step by Step Tutorials

Concepts & Architecture

15 Minutes Tutorial

Advanced Topics

API

Known WebServer Issues

References

External Documentations

githalytics.com alpha

Clone this wiki locally