Class BayeuxClient

All Implemented Interfaces:
Bayeux, ClientSession, Session, org.eclipse.jetty.util.component.Dumpable
Direct Known Subclasses:
OortComet

public class BayeuxClient
extends AbstractClientSession
implements Bayeux

BayeuxClient is the implementation of a client for the Bayeux protocol.

A BayeuxClient can receive/publish messages from/to a Bayeux server, and it is the counterpart in Java of the JavaScript library used in browsers (and as such it is ideal for Swing applications, load testing tools, etc.).

A BayeuxClient handshakes with a Bayeux server and then subscribes ClientSessionChannel.MessageListener to channels in order to receive messages, and may also publish messages to the Bayeux server.

BayeuxClient relies on pluggable transports for communication with the Bayeux server, and the most common transport is LongPollingTransport, which uses HTTP to transport Bayeux messages and it is based on Jetty's HTTP client.

When the communication with the server is finished, the BayeuxClient can be disconnected from the Bayeux server.

Typical usage:

 // Handshake
 String url = "http://localhost:8080/cometd";
 BayeuxClient client = new BayeuxClient(url, LongPollingTransport.create(null));
 client.handshake();
 client.waitFor(1000, BayeuxClient.State.CONNECTED);

 // Subscription to channels
 ClientSessionChannel channel = client.getChannel("/foo");
 channel.subscribe(new ClientSessionChannel.MessageListener()
 {
     public void onMessage(ClientSessionChannel channel, Message message)
     {
         // Handle the message
     }
 });

 // Publishing to channels
 Map<String, Object> data = new HashMap<String, Object>();
 data.put("bar", "baz");
 channel.publish(data);

 // Disconnecting
 client.disconnect();
 client.waitFor(1000, BayeuxClient.State.DISCONNECTED);