I have written a few entries about a JavaFX chat client and about a Comet Chat engine. Now, I have put together the simplest examples I could come up with.
Both the ChatServer (using Comet) and the ChatClient (in JavaFX) can be downloaded here.
ChatServerdirectory contains a Netbeans project that uses Comet in GlassFish v2. If you want to run this example in GlassFish v3, you have to change the package name
com.sun.grizzly.comet.CometContext. The ChatServer contains 3 classes:
init(ServletConfig config)method. Incoming requests have a parameter
method. This parameter can have two values: listen and chat.
HttpServletResponseis attached to the
CometContextWhen new chat entries are received, the CometHandler will send the content to the attached writer.
CometHandler. When a client starts listening, a new instance of the
is created, and the
PrintWriterconnected to the ServletResponse is stored. When new chat entries are received, the
onEventmethod will be called, and the chat text will be written to the
HttpRequestfor calling a url containing the chat entry information. We can't use the same approach for reading incoming chat entries, since the
onInputfunction of the
HttpRequestis only called when all input has been read. The existing
javafx.datapackages are very suitable for reading message-based content (i.e. RSS feeds), but not for streaming. Hence, we created a mechanism where a callback interface (
ChatInputis called each time the InputStream receives a new chat entry). This is done by means of the
FXReaderJavaFX class and the
AsyncReaderjava class. Again, for implementation details have a look at my previous blog entry.
At LodgON, we are working on a product that contains chat functionality. It is more complex than the example we've shown here, i.e. the chat can be moderated, users can have different roles, documents can be shown,... . The basic principles of the chat interactions are the same, though.
written on 14 Sep 2009 12:23.Create comment