Additional Libraries
Here you can find a set of complementary libraries
Json
The Json library allows easy and fast object serializations<./p>
API
The Json library has a single static method:
public static String json(Object o)
This method returns the JSON representation of the object (o).
Comet
Comet (aka ajax push) allows the server to "push" messages to the client. The comet library handle all the communication tasks and lets you send events with minimum effort.
API
The Comet class has two static methods:
void send(HttpSession s, String ch, Object msg)
This method sends a message (msg) to a specific client (s) on a specific channel (ch)
void reset(HttpSession s, String ch)
This method resets all the messages sent to the client.
Settings
To use the comet library you need to do the following:
- Download and copy the library to your lib folder.
- Copy the following code to your web.xml file.
<listener> <listener-class>comet.Comet</listener-class> </listener> <servlet> <servlet-name>comet</servlet-name> <servlet-class>comet.Comet</servlet-class> </servlet> <servlet-mapping> <servlet-name>comet</servlet-name> <url-pattern>/comet/*</url-pattern> </servlet-mapping>
<Connector URIEncoding="UTF-8" connectionTimeout="5000" keepAliveTimeout="300000" port="80" protocol="org.apache.coyote.http11.Http11AprProtocol" redirectPort="8443"/>
Client Side
The server can't really push messages to the client, instead the client has to listen to the server's messages. To do that you need to send a request with the following format
/comet/<channel>?mid=<mid>
Where channel is the channel the clients listen to and mid is the message number the client wants. A typical client's code, using the jj library, should look like that:
var mid = 0;
function listen(m) {
if (m && m.mid == mid) {
mid++;
var msg = m.msg;
//do something with msg
}
jj("/comet/news", {
mid : mid
}, listen);
}
listen();