Using the SDK in Web
Danny Coward, December 2011
So far you have only used the SDK in standalone mode. This is
useful when learning about Web Sockets and the SDK API, but its
much more useful to be able to use web sockets within the larger
context of the Java EE platform. In particular, web sockets that
are part of a web application would like to know who is logged in,
and any other information held in the HttpSession for each user of
the web application. Or some people might even like to expose
their EJBs as web sockets !
The SDK can also run as an extension to Glassfish, for now
embedded in the web container. Maybe coming to the EJB container
too, we'll see.
I'll outline below how you can create Java EE applications that
run on the Web Sockets SDK running in Glassfish. I'll do it bu
helping you run the Group Trading application that uses
HttpSessions to coordinate the actions of three separate web
socket end points embedded in the Group Trading web page.
Building and running the Group
This is a Java EE NetBeans project that contains the Group Trading
application and co-packages the Web Sockets SDK. The SDK in turn
depends on the web sockets implementation in Grizzly, which is of
course part of this release of Glassfish.
2. Resolve Dependencies
The application depends on the web
: the API (WS_API
and WS_Platform_1946 JARs
See the Getting Started Guide
for where to get those if you don't already know.
Also, it depends on a couple of the websockets JARs from your
Finally it depends on the a JSON
for parsing in the drawing sample. So you'll need to
go build the JAR from http://json.org/java/
You will also need to make sure that the build process is picking
up the annotations in the sample.
In NetBeans, go to the project properties dialog, select
Build->Compiling. Check the boxes to enable annotation
processing, and enable annotation processing in the editor, turn
off Compile on Save, and add websockets.platform.processors.WSProcessor
to the list of annotation processors.
3. Configure Glassfish
First, you need to turn on the native support for Web Sockets in
Glassfish. You can do in the admin tool.
asadmin set configs.config.server-config.network-config.protocols.protocol.http-listener-1.http.
Secondly and lastly, you'll need to tell Glassfish about the users
of the application. You'll need to add a couple of
username/password combinations to Glassfish. Using the admin tool,
add two users to the file realm, and make them part of the
'managers' role. Before you proceed, you'll need to make sure that
you make a mapping in the glassfish descriptor
WEB-INF/glassfish-web.xml in the application something like this:-
Then, deploy the application onto Glassfish, login with one of the
username passwords, and enjoy.