Search |
||
Writing a Comet web application using GlassFishPosted by jfarcand on October 31, 2006 at 7:09 PM PST
This blog describes how to write Comet enabled web application using GlassFish's Comet Engine. A couple of months ago, I've blogged about the technical details of the GlassFish's Comet support. Since then, I've got a lot of feedbacks on the blog and also privately. Surprisingly, a lot of peoples have started using the API and an asked for a blog describing a basic example. So here it comes ... a basic Chat Servlet :-)
First, to enable Comet Support in GlassFish, add the following in ${glassfish.home}/domains/domain1/config/domain.xml Next, add in you web.xml: OK now the interesting parts. The first things to decide when writing a Comet enabled web app is the component that will get polled. For this example, I will use a Servlet. First, the Servlet needs to register to the CometEngine: The important part to define first is the context path that will be considered for Comet processing (or polling). All requests that takes the form of http:// Next, you need to define a Comet request Handler which will get invoked every time the CometContext is updated. For the Chat, the handler will be created after the user has entered its user name (by issuing http://.../login.jsp) After the user has logged in, the browser will be redirected to the chat.jsp page, which will sent the action="openchat". The CometHandler (the class that will update the chat message box) implementation looks like: Every time the user will post a new message, the CometHandler.onEvent(...) will be invoked and the Chat message pushed back to the browser. On the client side, the chat.jsp page looks like You can download the application (which include the src) here. Note that the application described here is really to give an example. I would never recommend the use of static variables like I did in the example.
Before I forgot, one interesting feature I've recently added (was requested on first blog on Grizzly's Comet) is the ability to update a single CometHandler (or a single polled request). When calling cometContext.addCometHandler(..), the returned value can be later re-used to push datas only to that cometHandler by doing:
See the API for more info. For the Chat example, I've added a pop up window where a chat moderator receives all the chat messages, who is connected and from where: That's it. Very simple, is it? No needs to spawn a thread anywhere on the Servlet side, no special Servlet operations, etc. Once I've a chance, I will try to use AJAX and improve the client. Any help is appreciated on that side :-) As usual, thanks for all the feedbacks sent by emails! »
Related Topics >>
Java Enterprise Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|
scrollbar