Monday, June 28

Glassfish/Grizzly WebSocket

Hi,
I'm trying to get a WebSocket server application working using Glassfish 3.1.
There is very little information about how to do it since both Glassfish 3.1 and WebSocket are not finished yet. The best source I've found so far is Antwerkz, Inc. which like this post got out-of-date pretty quickly.

I'll try to publish here the things I did manage to get working hope it will help other in the way.

I'm currently using Glassfish 3.1 nightly build (glassfish-3.1-b07-06_27_2010) and NetBeans 6.8 development release (201006130001). I'm working on Windows XP Pro machine.

I've copy the following files from glassfish\modules into my project\lib:
  • grizzly-websockets.jar
  • grizzly-utils.jar
  • grizzly-http.jar
Those files needed because I use classes from them in my project code.

Another important thing (and easy to forget) is to turn on web socket support in grizzly by running this command:
asadmin set configs.config.server-config.network-config.protocols.protocol.http-listener-1.http.websockets-support-enabled=true

I've used a servlet to register my WebSocket application.

This is the code for the servlet:
public class WebSocketServlet extends HttpServlet {

private static final Logger logger = Logger.getLogger("WebSocketServlet");

@Override
public void init(ServletConfig config) throws ServletException {
ChatApplication app = new ChatApplication();
String appUri = config.getServletContext().getContextPath() + "/chat";
WebSocketEngine.getEngine().register(appUri, app);
//WebSocketEngine.getEngine().register(config.getServletContext().getContextPath() + "/chat", app);
}

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {


super.service(req, res);
}


}


In order for this servlet will actually get initialized we have to change web.xml file in WEB-INF:

<servlet>
<servlet-name>WebSocketServlet</servlet-name>
<servlet-class>websocketchat.WebSocketServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>


Now we left with implement ChatApplication and ChatWebSocket which I will cover once I'll actually get them to work.

The things I can't understand yet:
  1. How is responsible to set isConnected on the socket? is it me or the framework?
  2. How to broadcast message to several (all or part) sockets without suffer from delay due to closed or unresponsive sockets?

Hope it helps,
Ido


No comments:

Post a Comment