【问题标题】:Server push using COMETD to client(dojo)服务器使用 COMETD 推送到客户端(dojo)
【发布时间】:2012-09-06 07:43:18
【问题描述】:

我正在尝试将消息从服​​务器推送到客户端。我正在使用 DOJO 1.7、Cometd 和与 tomcat6 集成的 Jetty。

 //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");
            });

我想向订阅特定频道的所有客户端广播消息。当 m 使用 remore.deliver 时,它正在向单个客户端发送消息,而不是向订阅该频道的所有客户端发送消息。 channel.publish 对我不起作用...任何帮助和 cmets 都非常感谢。

【问题讨论】:

    标签: java javascript dojo comet cometd


    【解决方案1】:

    您的客户端正在发布到频道/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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多