A GWT module that wraps the CometD JavaScript client library.
Clone the repository and import the gwt-cometd
project into your Eclipse workspace. Then add the gwt-cometd
project to the build path of your GWT module.
Add the following entry to the .gwt.xml
file of your GWT module:
<inherits name="org.lutzmann.gwt.cometd.gwtcometd"/>
CometDConfiguration config = new CometDConfiguration("http://127.0.0.1:8080/cometd");
config.setLogLevel(LogLevel.debug);
...
CometD cometd = new CometD(config);
Subscription connectSubscription = cometd.addMetaListener(MetaChannel.connect,
new CometDListener<BayeuxMessage>() {
@Override
public void processMessage(BayeuxMessage message) {
if (message.isSuccessful()) {
// connected
...
} else {
// disconnected
...
}
}
});
...
cometd.removeListener(connectSubscription);
cometd.handshake();
Subscription quoteSubscription = cometd.subscribe("/FX/EURCHF",
new CometDListener<BayeuxMessage>() {
@Override
public void processMessage(BayeuxMessage message) {
JSONObject data = new JSONObject(message.getData());
JSONNumber bid = (JSONNumber) data.get("bid");
JSONNumber ask = (JSONNumber) data.get("ask");
...
}
});
...
cometd.unsubscribe(quoteSubscription);
JSONObject data = new JSONObject();
data.put("bid", new JSONNumber(1.2011));
data.put("ask", new JSONNumber(1.2014));
cometd.publish("/FX/EURCHF", data.getJavaScriptObject());
cometd.startBatch();
...
cometd.endBatch();
NOTE: Do not forget to call endBatch()
after a call to startBatch()
!
cometd.unregisterTransport(Transport.websocket);
For details on the described methods here have a look at the excellent CometD Reference Manual.
- Bernhard Lutzmann
- Ondrej Galik
- Julien Faucher
Thanks!