Comet in Glassfish/Grizzly with Ajax or iframe
The truth is that the example of Jean-Francois seemed to me too complex and I'm little missed by the use of an iframe as the Comet client ... when everybody speak about AJAX and the possibilities of Comet, it seemed to me strange that we are using it "again".
The truth is that everything about Comet is very new and there is not two much information on the Internet, but I could have found interesting things and reasons for which using iframes or AJAX as client of Comet. I have to warn, that i'm not very good with javascript, but I know a little and they have always spoken me very well about Prototype, so the target was to creat an AJAX client using this framework. Lets' go:
We already saw that the implementation of Comet in Grizzly is very simple, but I will repeat those explanation on my example that has less functionalities.
1.-We register the comet under a name. It can share a URL to give sense to the operations but it is not mandatory. This operation must be realized before no other, fso its natural place is the method init of a Servlet. If we also configured the servlet with the property <load-on-startup>0</load-on-startup> will garantize that CometContext will be created before it receive any connection.
public void init (ServletConfig config) throws ServletException
{
super.init (config);
//comet = servlet context path but it is not obligatory
contextPath = config.getServletContext ()
.getContextPath () + "/subscription";
CometEngine cometEngine = CometEngine.getEngine ();
context = cometEngine.register (contextPath);
context.setExpirationDelay (40 * 1000); // seconds of delay
}
2.-The Servlet is only going to realize two operations: register the client in the Comet and send notifications to all the clients connected. To send the notification it must send the parameter notify to the Servlet. To guarantee that a client does not try to get connected two times, the example create a session attribute that verifies it
protected void doPost (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
if (request.getParameter (to "notify") == null
&& request.getSession () .getAttribute ("connected") == null)
{// the client tuned in to the comet
response.setContentType ("text/html"); // very important
EjemploCometHandler handler = new EjemploCometHandler ();
andler.attach (response.getWriter ());
int handlerId = context.addCometHandler (handler);
request.getSession () .setAttribute ("connected",
Integer.valueOf (handlerId));
response.getWriter () .print (" Connected to the Commet
");
response.getWriter () .flush ();
//we do not close the connection .. that about that talks each other
} else if (request.getParameter (to "notify")! =null) {
context.notify (" He Sent all the users connected to the comet
");
}
return;
}
3.-The CometHandler implemented for the example limits itself to sending the notification messages and report the user that is disconnected.
public class EjemploCometHandler implements CometHandler{
deprive yourself PrintWriter printWriter=null;
public void attach (PrintWriter printWriter) {
this.printWriter = printWriter;
}
public void onEvent (CometEvent event) throws IOException {
printWriter.println (event.attachment ());
printWriter.flush ();
}
public void onInitialize (CometEvent event) throws IOException {
printWriter.println ("onInitialize");
printWriter.flush ();
}
public void onTerminate (CometEvent event) throws IOException {
printWriter.println ("onTerminate");
printWriter.flush ();
printWriter.close ();
}
public void onInterrupt (CometEvent event) throws IOException {
printWriter.println ("onInterrup");
printWriter.flush ();
printWriter.close ();
}
}
This is the program taht run on the sever, now we go to the client. There are two possibilities AJAX or iframe. Everything points that the best option is the iFrame, for several reasons:
- An alone navigator can open two HTTP connections HTTP from on page/frame, so if we support spend one for the Comet, we only have one free to make request.
- The iframe works in all the navigators whereas AJAX in InternetExplorer seems to have problems with Comet, since this navigator does not realize the information that is coming until the connection is closed:-(
Let's see the code of the example of the client with the iframe, on the one hand we have the iframe that gets connected to the comet. The button realizes Ajax call that update the iframe of the same page and that of all the connected clients:
<html>
<head> <title> Example Comet iFrame </title>
<script src = "prototype.js" type = "text/javascript"> </script>
</head>
<body>
<iframe src = "subscription" yam = "comet" width = " 100 % "> </iframe>
<script type = "text/javascript" language = "javascript">
// <![CDATA [
function notificarComet () {
var url = 'subscription';
new Ajax. Request (url, {
method: 'get',
parameters: 'notificar=true'
});
}
// ]]>
</script>
<unputt type = "button" yam = "a" values = to "notify"
onclick = " notificarComet (); "/>
</body>
</html>
The only form that I have found to make them same with Prototype and knowing that only works in firefox it is the above code. After doing the request and connect with the Comet, it is necessary to introduce the AJAX process client in a bucle that is verifying if any information has been sent from the Comet:
<html>
<head> <title> Example Comet Ajax </title>
<script src = "prototype.js" type = "text/javascript"> </script>
<script type = "text/javascript" language = "javascript">
// <![CDATA [
function comet () {
var url = 'subscription';
var listener=null;
new Ajax. Request (
url,
{onInteractive: function (xhr) {
if (! listener) {
listener=new PeriodicalExecuter (
function () {
$('update') .update (xhr.responseText);
},
1 /* check for changes every 1 second*/
);
}
}
}
);
}
function notificarComet () {
var url = 'subscription';
new Ajax. Request (url, {
method: 'get',
parameters: ' notificar=true*t = ' + new Dated () .getTime ()
});
}
// ]]>
</script>
</head>
<body onload = " comet (); ">
<div go = "update"> </div>
<unputt type = "button" yam = "a" values = to "notify"
onclick = " notificarComet (); "/>
</body>
</html>
It does not seem that Prototype has been prepared for Comet. Nevertheless another very well-known framework javascript, Dojo, is very well integrated with one Comet sever, www.cometd.org, and the Dojo operation dojo.io.bind () is built thinking on comet. I am afraid it will be necessary to try in next chapters;-). Meanwhile I leave to you the example so that you can prove it.





