java - CometD 将消息发布回客户端

标签 java comet cometd

我在向客户端发回消息时遇到问题。下面是我的代码

JavaScript

dojox.cometd.publish('/service/getservice', {
                        userid : _USERID,

                    });
dojox.cometd.subscribe('/service/getservice', function(
            message) {
        alert("abc");
        alert(message.data.test);
    });

Configuration Servlet

bayeux.createIfAbsent("/service/getservice", new ConfigurableServerChannel.Initializer() {

        @Override
        public void configureChannel(ConfigurableServerChannel channel) {
            channel.setPersistent(true);
            GetListener channelListner = new GetListener();
            channel.addListener(channelListner);
        }
    });

GetListener类

public class GetListener implements MessageListener {
 public boolean onMessage(ServerSession ss, ServerChannel sc) {
      SomeClassFunction fun = new SomeClassFunction;
}
}

一些类函数

class SomeClassFunction(){

}

这里我创建一个 boolean 变量 boolean 成功; 如果是 true,则向客户端发送一条 javascript 消息。如何将消息发送回客户端。我也尝试过这条线。

      remote.deliver(getServerSession(), "/service/getservice",
                    message, null);

但它在远程对象和 getServerSession 方法上给了我一个错误。

最佳答案

为了实现您的目标,您不需要实现监听器,也不需要配置 channel 。您可能需要在稍后阶段添加一些配置,例如为了添加授权者。

这是 ConfigurationServlet 的代码,取自此 link :

public class ConfigurationServlet extends GenericServlet
{
    public void init() throws ServletException
    {
        // Grab the Bayeux object
        BayeuxServer bayeux = (BayeuxServer)getServletContext().getAttribute(BayeuxServer.ATTRIBUTE);
        new EchoService(bayeux);
        // Create other services here

        // This is also the place where you can configure the Bayeux object
        // by adding extensions or specifying a SecurityPolicy
    }

    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
    {
        throw new ServletException();
    }
}

这是 EchoService 类的代码,取自此 link :

public class EchoService extends AbstractService
{
    public EchoService(BayeuxServer bayeuxServer)
    {
        super(bayeuxServer, "echo");
        addService("/echo", "processEcho");
    }

    public void processEcho(ServerSession remote, Map<String, Object> data)
    {
        // if you want to echo the message to the client that sent the message
        remote.deliver(getServerSession(), "/echo", data, null);

        // if you want to send the message to all the subscribers of the "/myChannel" channel
        getBayeux().createIfAbsent("/myChannel");
        getBayeux().getChannel("/myChannel").publish(getServerSession(), data, null);
    }
}

关于java - CometD 将消息发布回客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8519878/

相关文章:

java - Tomcat 7 上的 Servlet 3 异步任务

java - Spring MVC WebApp 中的推送通知

hadoop - cumulocity中的java客户端如何监听事件?

java - 如何获取文档字段的 TokenStream 以用于突出显示?

PHP comet usleep 阻塞 apache mpm?

perl - 使用 Apache 和 Perl 进行服务器推送的机制

java - Cometd 过滤 channel 中特定客户端的数据

java - JSP XML 语法错误 : JasperException when replacing <% %> with <jsp:scriptlet> </jsp:scriptlet>

Java.随机环绕

java - 当值包含一个列表并且对列表的引用为 NULL 时,如何在 JSON 中迭代一个 Map