Comet-Jetty(4)Read the Source Codes and Refactor the Project

本文详细介绍了如何阅读 Comet-Jetty 项目的源码,并对其进行了重构。主要内容包括安装源码到本地仓库,创建易于操作的项目模块(easyTalker、easyCometd、easyAndroidSpeaker),实现页面消息、离线消息和私密消息的处理。通过实例展示了如何使用 BayeuxClient 进行客户端配置和消息发布,以及遇到的常见错误提示和解决方案。重点在于提供了一个全面的指南,帮助开发者理解和改进 Comet-Jetty 的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Comet-Jetty(4)Read the Source Codes and Refactor the Project

1. Find resources
Install the source to my repository, then it is easy for me to read the source codes.
>mvn -DskipTests=true source:jar install

2. Create new Project easytalker/easycometd
easycometd will handle the page messages.
easytalker will handle the offline messages.
easyandroidspeaker will handle the android client in the feature.

The client Class will be as follow to support the private talk message:
public void go() throws InterruptedException {
String defaultURL = "http://localhost:8080/easycometd/cometd";
String url = defaultURL;

client = new BayeuxClient(url, LongPollingTransport.create(null));
client.getChannel(Channel.META_HANDSHAKE).addListener(
new InitializerListener(nickname, client, chatListener,
membersListener));
client.getChannel(Channel.META_CONNECT).addListener(
new ConnectionListener(nickname, client));

client.handshake();

boolean success = client.waitFor(2000, BayeuxClient.State.CONNECTED);
if (!success) {
System.err.printf("Could not handshake with server at %s%n", url);
return;
}

String text = "hello,where is the book";

Map<String, Object> data = new HashMap<String, Object>();
data.put("user", nickname);
data.put("chat", text);
client.getChannel("/chat/demo").publish(data);

Map<String, Object> pdata = new HashMap<String, Object>();
text = "hello,where is the money";
pdata.put("chat", text);
pdata.put("room", "/chat/demo");
pdata.put("user", "karl");
pdata.put("peer", "sillycat");
client.getChannel("/service/privatechat").publish(pdata);

for (int i = 0; i < 10; i++) {
Thread.sleep(5000);
}

data = new HashMap<String, Object>();
data.put("user", nickname);
data.put("membership", "leave");
data.put("chat", nickname + " has left");
client.getChannel("/chat/demo").publish(data);

client.getChannel("/service/privatechat").release();
client.getChannel("/chat/demo").release();
client.disconnect(3000);
}

3. Error Message Tips
Error Message
802 [HttpClient-13] INFO org.cometd.client.BayeuxClient.1188088890 - Messages failed [{id=7, connectionType=long-polling, channel=/meta/connect, clientId=33uitvf0nskcyrayr07o0zvdt}]
java.net.ProtocolException: Unexpected response 500: TransportExchange@2ae522a0=POST//localhost:8080/easycometd/cometd/connect#CONTENT(0ms)->COMPLETED(0ms)sent=23ms
at org.cometd.client.BayeuxClient$PublishTransportListener.onProtocolError(BayeuxClient.java:1161)
at org.cometd.client.transport.LongPollingTransport$TransportExchange.onResponseComplete(LongPollingTransport.java:324)
at org.eclipse.jetty.client.HttpExchange$Listener.onResponseComplete(HttpExchange.java:1153)
at org.eclipse.jetty.client.HttpExchange.setStatus(HttpExchange.java:300)

SEVERE: Servlet.service() for servlet [cometd] in context with path [/easycometd] threw exception
java.lang.IllegalStateException: Not supported.
at org.apache.catalina.connector.Request.startAsync(Request.java:1664)
at org.apache.catalina.connector.Request.startAsync(Request.java:1657)
at org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1023)
at org.eclipse.jetty.continuation.Servlet3Continuation.suspend(Servlet3Continuation.java:171)
at org.cometd.server.transport.LongPollingTransport.handle(LongPollingTransport.java:288)
at org.cometd.server.CometdServlet.service(CometdServlet.java:181)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.eclipse.jetty.servlets.CrossOriginFilter.handle(CrossOriginFilter.java:212)
at org.eclipse.jetty.servlets.CrossOriginFilter.doFilter(CrossOriginFilter.java:179)

solution
There is solution to deal with tomcat7. Maybe only some configuration changes, but I just use tomcat6.x to run my project, it is ok now.


references:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值