CometD will post a message to the client

I had a problem sending a message to a client. Below is my code

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

Class getlistener

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

SomeClassFunction

 class SomeClassFunction(){ } 

here I create a boolean success boolean; if true, send a message to the client, which is in javascript. how to send a message to a client. I also tried this line.

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

but it gives me an error on the remote object and getServerSession method.

+2
source share
1 answer

To achieve your goal, you don’t need to implement listeners or configure channels. You may need to add some configuration at a later stage, for example, 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 EchoService class code taken from 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); } } 
+3
source

Source: https://habr.com/ru/post/1385941/


All Articles