CometD publish a message back to a client

坚强是说给别人听的谎言 提交于 2019-12-04 17:56:46

In order to reach your goal, you don't need to implement listeners nor to configure channels. You may need to add some configuration at a later stage, for example in order to add authorizers.

This is the code for the ConfigurationServlet, taken from this 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();
    }
}

This is the code for EchoService class, taken fro this 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);
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!