Object obj = event.attachment();
try{
if (obj instanceof Data){
Data data = (Data)obj;
String sdata = data.getJSONData();
res.write(sdata);
res.flush();
event.getCometContext().resumeCometHandler(this);
}
} catch (Throwable t){
logger.log(Level.SEVERE,"Data.onEvent",t);
}
if (event.getType() == CometEvent.READ){
CometInputStream is = (CometInputStream)obj;
// XXX This is dangerous...
byte[] dataStream = new byte[2 * 8192];
is.setReadTimeout(2000);
while (is.read(dataStream) > 0){
}
String sdata = new String(dataStream).trim();
if (sdata.length() <=1) return;
// XXX What abou the header.
try{
final Verb verb =
VerbUtils.parse(JSONParser.parse(sdata));
// Notify our listener;
CometContext cometContext = event.getCometContext();
cometContext.notify(
BayeuxCometHandler.newCometdContext(req,res,verb),
CometEvent.NOTIFY,
(Integer)cometContext.getAttribute(
BayeuxCometHandler.BAYEUX_COMET_HANDLER));
event.getCometContext().removeAttribute(this);
} catch (Throwable t){
logger.log(Level.SEVERE,"Data.onEvent",t);
}
}