Java 使用COMETD到客户端(dojo)的服务器推送
我正在尝试将消息从服务器推送到客户端。我使用的是Dojo1.7、Cometd和Jetty与tomcat6集成Java 使用COMETD到客户端(dojo)的服务器推送,java,javascript,dojo,comet,cometd,Java,Javascript,Dojo,Comet,Cometd,我正在尝试将消息从服务器推送到客户端。我使用的是Dojo1.7、Cometd和Jetty与tomcat6集成 //Server side code public class notificationService extends AbstractService { public notificationService(BayeuxServer bayeux, String name) { super(bayeux, name); System.out.println("
//Server side code
public class notificationService extends AbstractService {
public notificationService(BayeuxServer bayeux, String name) {
super(bayeux, name);
System.out.println("Inside constrcutor of Notification Service");
addService("/notification", "processNotification");
}
public void processNotification(ServerSession remote,ServerMessage.Mutable message)
{
System.out.println("Inside process Notification");
Map<String,Object> response = new HashMap<String,Object>();
response.put("payload",new java.util.Date());
getBayeux().createIfAbsent("/notification");
getBayeux().getChannel("/notification").publish(getServerSession(),response,null);
//remote.deliver(getServerSession(),"/notification", response, null);
}
//Client Side Code (DOJO)
var cometd = dojox.cometd;
cometd.init("http://serverip:port/cometd")
cometd.publish('/notification',{ mydata: { foo: 'bar' } });
cometd.subscribe('/notification', function(message)
{
//alert("Message received" + message.data.payload);
//alert(message.data.payload);
alert("Message received");
});
//服务器端代码
公共类notificationService扩展了AbstractService{
公共通知服务(BayeuxServer bayeux,字符串名称){
超级(巴约,姓名);
System.out.println(“通知服务的内部承包商”);
addService(“/notification”、“processNotification”);
}
public void processNotification(ServerSession remote、ServerMessage.Mutable消息)
{
System.out.println(“内部进程通知”);
Map response=newhashmap();
response.put(“payload”,new java.util.Date());
getBayeux().createifabbsent(“/notification”);
getBayeux().getChannel(“/notification”).publish(getServerSession(),response,null);
//deliver(getServerSession(),“/notification”,response,null);
}
//客户端代码(DOJO)
var cometd=dojox.cometd;
cometd.init(“http://serverip:port/cometd")
publish('/notification',{mydata:{foo:'bar'});
cometd.subscribe('/notification',函数(消息)
{
//警报(“收到消息”+消息.数据.有效载荷);
//警报(消息、数据、有效载荷);
警报(“收到消息”);
});
我想向订阅特定频道的所有客户端广播消息。当m使用remore时,它正在向单个客户端发送消息,但不是向订阅该频道的所有客户端发送消息。channel.publish不适用于我…非常感谢任何帮助和评论。您的客户端正在发布到频道
/notification
,这是一个广播频道(有关定义,请参阅),这样,您的服务不仅可以接收消息,还可以向该频道的所有订户广播消息
然后在您的服务中调用ServerChannel.publish()
,它将向/notification
频道的订户发送另一条消息(除了服务本身,还有无限循环预防)
执行此类操作的更好方法是使用服务通道,例如/service/notification
,将初始消息从客户端发送到服务。
然后,该服务可以像现在一样向频道/notification
广播
调用ServerChannel.publish()
是从服务广播消息的正确方法。
不幸的是,你没有具体说明为什么他不为你工作,所以我帮不了你
首先,我将在第一条消息中使用客户机和服务之间的服务通道
还要注意,Tomcat6并不是CometD的最佳解决方案,因为它不支持异步Servlet(Servlet3)
您最好使用与Servlet3兼容的容器,例如