I've been having problems getting Comet working, so have pared down my
code to the absolute minimum. I've pasted the client HTML code, and
the server Java code, at the bottom.
All the example does is send lines of text typed on the server console
to the HTML/dojo client.
So, here's the questions:
1) This WORKS when I I access the file from a URL like: file:///C:/ajaxExample.html,
but DOESN'T work when I then server very same file served via tomcat
with a URL like:
http://myservername:8081/ajaxExample.html
I am 99.9% sure this is NOT a cross-domain issue, as I have added this
to my Firefox prefs:
user_pref("capability.policy.default.XMLHttpRequest.open",
"allAccess");
Also, I DO see on the server side that a new client has connected. The
ONLY difference I can see when looking at the requests in Firebug is
that the one served from a http:// URL makes a GET request, whereas
the one served from a file:/// URL does a POST request. I am pretty
sure that's the reason one works and the other doesn't. The question
therefore is a) Why? and b) How do I fix this? I am not even sure why
serving from HTTP rather than a static file should mean that a
different HTTP request method is used.
2) For the purposes of development, as mentioned above, I used the
Firefox pref to turn off cross-domain checking. However, when I come
to deploy I'll need this to work. I *thought* that latest dojo/cometd
automatically dealt with cross domain issues with <script> tags, but
it doesn't appear to work. Any ideas on this?
3) Finally, I just can't find any good documentation on any of the
following:
a) dojox.cometd.init
b) dojox.cometd.subscribe
c) org.mortbay.cometd.continuation.ContinuationCometdServlet
d) org.mortbay.cometd.BayeuxService
Any ideas where I'd get some comprehensive documentation on all the
methods, parameters, examples, and so forth please?
Thanks
Marcos
Here is the client:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "
http://www.w3.org/
TR/html4/strict.dtd">
<html>
<head>
<title>Ajax Example</title>
<script type="text/javascript" src="dojo/dojo/
dojo.js.uncompressed.js"></script>
<script type="text/javascript" src="dojo/dojox/
cometd.js.uncompressed.js"></script>
<script type="text/javascript">
dojo.require("dojox.cometd");
function setupComet(){
dojox.cometd.init('
http://myhostname:8080/cometd');
dojox.cometd.subscribe("/service/dev", callback);
}
dojo.addOnLoad(setupComet);
function callback(msg){
dojo.byId("callbacks").innerHTML = msg.channel + ": " +
msg.data;
}
</script>
</head>
<body>
<h1>Callbacks</h1>
<div id="callbacks"></div>
</body>
</html>
Here is the server:
package com.aboxo.ajaxpublisher;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.cometd.continuation.ContinuationCometdServlet;
import org.mortbay.cometd.continuation.ContinuationClient;
import org.mortbay.cometd.AbstractBayeux;
import org.mortbay.cometd.ClientImpl;
import org.mortbay.cometd.BayeuxService;
import dojox.cometd.Bayeux;
import dojox.cometd.Client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) throws Exception {
Server server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(8080);
server.addConnector(connector);
Context context = new Context(server, "/", Context.NO_SECURITY |
Context.NO_SESSIONS);
ContinuationCometdServlet cometdServlet = new
ContinuationCometdServlet();
ServletHolder cometdHolder =
setupCometdServletHolder(cometdServlet);
context.addServlet(cometdHolder, "/cometd/*");
server.start();
AbstractBayeux bayeux = cometdServlet.getBayeux();
bayeux.setJSONCommented(true);
new BasicBayeuxService(bayeux);
while (true) {
String input = getKeyboardInput();
java.util.Set<String> ids = bayeux.getClientIDs();
ClientImpl[] clients = new ClientImpl[ids.size()];
int i = 0;
for (String id : ids) {
clients[i] = (ClientImpl) bayeux.getClient(id);
if (clients[i] instanceof ContinuationClient) {
ContinuationClient cc = (ContinuationClient) clients[i];
cc.publish("/service/dev", "{text:'" + input + "'}", "option
msg ID");
}
i++;
}
}
}
private static String getKeyboardInput() {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String text = null;
try {
text = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read text!");
System.exit(1);
}
System.out.println("Text input:" + text);
return text;
}
private static ServletHolder
setupCometdServletHolder(ContinuationCometdServlet cometdServlet)
{
ServletHolder cometdHolder = new ServletHolder(cometdServlet);
cometdHolder.setInitParameter("timeout", "240000");
cometdHolder.setInitParameter("interval", "0");
cometdHolder.setInitParameter("maxInterval", "30000");
cometdHolder.setInitParameter("multiFrameInterval", "1500");
cometdHolder.setInitParameter("JSONCommented", "true");
cometdHolder.setInitParameter("logLevel", "1");
return cometdHolder;
}
public static class BasicBayeuxService extends BayeuxService {
public BasicBayeuxService(Bayeux bayeux) {
super(bayeux, "sendObject");
subscribe("/service/dev", "sendObject");
}
public Object sendObject(Client client, Object object) {
System.out.println("Sending to client:" + client + " the
following object:" + object);
return object;
}
}
}