[英]Server push using COMETD to client(dojo)
我正在尝试将消息从服务器推送到客户端。 我正在使用DOJO 1.7,Cometd和Jetty与tomcat6集成。
//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");
});
我想向订阅特定频道的所有客户广播消息。 当使用remore.deliver时,它会向个人客户端发送消息,但不会向订阅该频道的所有客户端发送消息。 channel.publish对我不起作用...非常感谢任何帮助和评论。
您的客户端正在发布到频道/notification
,这是一个广播频道(请参阅http://docs.cometd.org/reference/#concepts_channels以获取定义),以便该消息不仅会被您的服务接收,还会广播到该频道的所有订阅者。
然后在您的服务中,您调用ServerChannel.publish()
,它将向/notification
通道的订阅者发送另一条消息(除了服务本身 - 存在无限循环防止)。
执行此类操作的更好方法是使用服务通道(例如/service/notification
)将初始消息从客户端发送到服务。 然后,服务可以像现在一样广播到频道/notification
。
调用ServerChannel.publish()
是从服务广播消息的正确方法。 不幸的是,你没有明确说明为什么不适合你,所以我无能为力。
我首先在客户端和服务之间使用第一条消息的服务通道。
另请注意,Tomcat 6并不是CometD的最佳解决方案,因为它不支持异步servlet(Servlet 3)。
最好使用符合Servlet 3的容器,例如Jetty 8 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.