Cometd javascript客户端不订阅广播频道

时间:2014-02-07 19:33:19

标签: java spring dojo cometd

似乎javascript客户端无法订阅广播频道或从中接收消息。以下是spring-cometd服务代码,它在收到来自外部事件的消息后在通知频道上广播消息。 java-cometd客户端可以非常接收广播消息。即使是javascript客户端也可以在服务频道上发布和订阅消息,但不能在广播频道上发布和订阅消息。订阅在握手后完成。

JavaScript代码:

  var cometd = $.cometd;



  cometd.addListener('/meta/handshake', _metaHandshake);// handshake listener
        cometd.addListener('/meta/connect', _metaConnect);//connection connect listener
        cometd.addListener('/meta/disconnect', _metaDisconnect); 
        cometd.handshake();



  function _metaHandshake(handshake)
            {
                if (handshake.successful === true)  
                {


                    cometd.batch(function()
                    {


                cometd.subscribe('/notification', function(m) {alert("hi"); });



                    });
                }

当javascript客户端订阅广播频道时会出现什么问题。

@javax.inject.Named // Tells Spring that this is a bean
@javax.inject.Singleton // Tells Spring that this is a singleton
@Service("notificationService")
public class NotificationService {

    private static final String channelName="/notification";
    private static ServerChannel serverChannel;
    private Logger logger = Logger.getLogger(this.getClass());

    @Inject
    private BayeuxServer bayeuxServer;

    @Session
    private LocalSession session;



    @PostConstruct
    public void init()
    {
        logger.debug("Notification Service Initialized");
        channelSetUp();
        session = bayeuxServer.newLocalSession("external");
        session.handshake();

    }


    public void channelSetUp()
    {

        MarkedReference<ServerChannel> channelCreated = bayeuxServer.createChannelIfAbsent(channelName, new ServerChannel.Initializer()
        {
        public void configureChannel(ConfigurableServerChannel channel)
        {
            channel.setPersistent(true);// channel persistent
            channel.addAuthorizer(GrantAuthorizer.GRANT_SUBSCRIBE_PUBLISH); 
        }
        });

        if(channelCreated.isMarked())
        {
            serverChannel = bayeuxServer.getChannel(channelName);

        }
    }






    public void onExternalEvent( Map<String, Object> data)
    {


        // ServerChannel serverChannel = this.bayeuxServer.getChannel(channelName);

    // logger.debug("Notify MessageData from JMS ::" + data.toString());
    if (serverChannel != null)
        {
           // Broadcast the data
        serverChannel.publish(session, data, null);

        }


    }



    @Listener(Channel.META_SUBSCRIBE)  
    public void processSubscription(ServerSession remote, ServerMessage message)
    {   
        // What channel the client wants to subscribe to ?
       String channel = (String)message.get(Message.SUBSCRIPTION_FIELD);
       logger.debug("Client Channel ::"+channel);

    }




}

1 个答案:

答案 0 :(得分:0)

您的客户端代码是正确的。

您的服务器代码可以改进,特别是:

  • 您无需在init()中创建本地会话:@Session注释会为您完成。
  • 您无需在channelSetup()中致电init():只需使用@Configure注释。
  • 您无需存储对ServerChannel的引用:由于您已将频道设为永久性,因此onExternalEvent()只需执行:bayeuxServer.getChannel(channelName).publish(...);

如果您正确地遵循了Spring integration的文档,并且避免了在文档中描述的2 BayeuxServer个实例的创建(使用Spring时的典型错误),那么您应该很高兴去

我建议您在客户端和服务器中enable debug logging,并查看日志,这可以解释为什么您的JavaScript客户端不会收到消息。

希望有所帮助!