Wierd comet issues - working coding supplied

2 views
Skip to first unread message

Marcos

unread,
Feb 15, 2008, 6:59:06 AM2/15/08
to cometd-dev
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;
}

}

}

Brendon Hogger

unread,
Feb 19, 2008, 12:35:08 PM2/19/08
to comet...@googlegroups.com
> 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.

From the paste, it looks like you're connecting to a cometd server on
a different port than the HTML file was served from. cometd will treat
this as cross-domain, regardless of your Firefox preferences, and try
to use (I guess) callback-polling (GET and <script> tags) instead of
XHR posts.

Why this would cause problems, I don't know.

Cheers,
Brendon

Greg Wilkins

unread,
Feb 19, 2008, 5:28:27 PM2/19/08
to comet...@googlegroups.com

because I recently broke jsonp cross domain support..... fixed in
svn now and in a release coming soon.

cheers

Marcos

unread,
Feb 22, 2008, 4:57:25 AM2/22/08
to cometd-dev
Any ideas when this will be available?

Do you have a nightly build? If not, where can I download the cometd/
bayeux extras source? Did not appear to be in there

Marcos

unread,
Feb 22, 2008, 5:20:22 AM2/22/08
to cometd-dev

Marcos

unread,
Feb 22, 2008, 5:36:27 AM2/22/08
to cometd-dev
Oh great - but there's nothing in org.mortbay.cometd.client

ARGHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

On Feb 19, 10:28 pm, Greg Wilkins <gr...@mortbay.com> wrote:

Greg Wilkins

unread,
Feb 25, 2008, 3:59:03 PM2/25/08
to comet...@googlegroups.com

Marcos,

sorry.... trying to get a 6.1.8 build out this week.

but if you have svn and mvn, then just checkout

https://svn.codehaus.org/jetty-contrib/jetty/branches/jetty-6.1/contrib/cometd

and type

mvn

in the top level

Note that everything does not appear when you browse the tree because
there are cross mounts.

cheers

Reply all
Reply to author
Forward
0 new messages