Comet Jetty(2)Go through the document

Comet Jetty(2)Go through the document

5. Java Libraries
5.3. Client Library
We can use CometD client implementation in any J2EE application. It consists of one main class, org.cometd.client.BayeuxClient, which implements the org.cometd.bayeux.client.ClientSession interface.

5.3.1. Handshaking
BayeuxClient.handshake()

HttpClient httpClient = new HttpClient();
//Here set up Jetty's HttpClient, for example:
// httpClient.setMaxConnectionsPerAddress(2);
httpClient.start();
// Prepare the transport
Map<String, Object> options = new HashMap<String, Object>();
ClientTransport transport = LongPollingTransport.create(options, httpClient);

ClientSession client = new BayeuxClient("http://localhost:8080/cometd", transport);

5.3.2. Subscribing and Unsubscribing
client.getChannel(CHANNEL).subscribe(fooListener);
client.getChannel(CHANNEL).unsubscribe(fooListener);

5.3.3. Publishing
Map<String, Object> data1 = new HashMap<String, Object>();
// Fill in the data1 map object
client.getChannel("/game/table/1").publish(data1);

5.3.5. Client Transports
// Prepare the WebSocket transport
WebSocketClientFactory wsFactory = new WebSocketClientFactory();
wsFactory.start();
ClientTransport wsTransport = new WebSocketTransport(null, wsFactory, null);

// Prepare the HTTP transport
HttpClient httpClient = new HttpClient();
httpClient.start();
ClientTransport  httpTransport = new LongPollingTransport(null, httpClient);

// Configure the BayeuxClient, with the websocket transport listed before the http transport
BayeuxClient client = new BayeuxClient("http://localhost:8080/cometd", wsTransport, httpTransport);

// Handshake
client.handshake();

5.4. Server Library
<servlet-name>cometd</servlet-name>
<servlet-class>org.cometd.server.CometdServlet</servlet-class>

The purpose for this is to perform cross-domain JavaScript requests.
<filter-name>cross-origin</filter-name>
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>

Annotated Style
@org.cometd.annotation.Service("echoService")
public class EchoService
{
    @javax.inject.Inject
    private BayeuxServer bayeux;
}

Inherited Style
public class EchoService extends AbstractService
{
    public EchoService(BayeuxServer bayeux)
    {
        super(bayeux, "echoService");
    }
}

5.4.2.4 Services Integration with Spring
    <bean id="otherService" class="com.acme..." />

    <bean id="bayeux" class="org.cometd.server.BayeuxServerImpl" init-method="start" destroy-method="stop">
        <property name="options">
            <map>
                <entry key="logLevel" value="3" />
                <entry key="timeout" value="15000" />
            </map>
        </property>
        <property name="transports">
            <list>
                <bean id="websocketTransport" class="org.cometd.websocket.server.WebSocketTransport">
                    <constructor-arg ref="bayeux" />
                </bean>
                <bean id="jsonTransport" class="org.cometd.server.transport.JSONTransport">
                    <constructor-arg ref="bayeux" />
                </bean>
                <bean id="jsonpTransport" class="org.cometd.server.transport.JSONPTransport">
                    <constructor-arg ref="bayeux" />
                </bean>
            </list>
        </property>
    </bean>

    <bean id="echoService" class="com.acme.cometd.EchoService">
        <constructor-arg><ref local="bayeux" /></constructor-arg>
        <constructor-arg><ref local="otherService" /></constructor-arg>
    </bean>

    <bean class="org.springframework.web.context.support.ServletContextAttributeExporter">
        <property name="attributes">
            <map>
                <entry key="org.cometd.bayeux">
                    <ref local="bayeux" />
                </entry>
            </map>
        </property>
    </bean>

5.4.3 Authorization
public class CustomSecurityPolicy extends DefaultSecurityPolicy
{
    public boolean canHandshake(BayeuxServer server, ServerSession session, ServerMessage message)
    {
        // Always allow local clients
        if (session.isLocalSession())
            return true;

        // Implement custom authentication logic
        boolean authenticated = authenticate(server, session, message);

        return authenticated;
    }
}
org.cometd.bayeux.server.SecurityPolicy
org.cometd.server.DefaultSecurityPolicy

5.6. Scalability Clustering with Oort
Any CometD server can become an Oort comet by configuring an instance of org.cometd.oort.Oort.

Oort clustering is not a high availability clustering solution: if one of the nodes crashes, then all the clients are disconnected and reconnect to other nodes (with a new handshake).

<servlet>
        <servlet-name>oort</servlet-name>
        <servlet-class>org.cometd.oort.OortStaticConfigServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
        <init-param>
            <param-name>oort.url</param-name>
            <param-value>http://host1:port/context/cometd</param-value>
        </init-param>
        <init-param>
            <param-name>oort.cloud</param-name>
            <param-value>http://host2:port/context/cometd,http://host3:port/context/cometd</param-value>
        </init-param>
</servlet>

6. Extensions
...snip...
references:
http://www.ibm.com/developerworks/cn/web/wa-lo-comet/
http://hi.baidu.com/ztf704/blog/item/82674a08328881980b7b8266.html
http://www.ibm.com/developerworks/cn/web/wa-cometjava/?cmp=dwskl&cpb=dw&ct=dwcon&cr=cn_51CTO&ccy=cn
http://haidii.iteye.com/blog/481004
http://dynamiclu.iteye.com/blog/486533
http://akalius.iteye.com/blog/192727
http://wiki.eclipse.org/Jetty/Feature/Stress_Testing_CometD
http://magicgod.iteye.com/blog/741171

http://docs.cometd.org/reference/#d0e117




你可能感兴趣的:(document)