Implementation in cometD

I am new to CometD. Is there a simple example for implementing a service channel model in the case of a response / request model. I saw cometd.org, but there is no example how to send an answer if I publish to any channel.

This is the client side.

alert("channel published1"); dojox.cometd.publish('/service/getlist'); alert("channel published"); dojox.cometd.subscribe('/service/getlist', function(message) { alert(message); }); 

this is the server side "ConfigurationServlet"

 bayeux.createIfAbsent("/service/getlist", new ConfigurableServerChannel.Initializer() { //new EchoService(bayeux); @Override public void configureChannel(ConfigurableServerChannel channel) { /*channel.setPersistent(true); GetListChannelListener channelListner = new GetOrderListChannelListener(); channel.addListener(channelListner);*/ new EchoService(bayeux); } }); 

EchoService

 public class EchoService extends AbstractService{ public EchoService(BayeuxServer bayeuxServer) { super(bayeuxServer, "getlist"); addService("/service/getlist", "processEcho"); } public void processEcho(ServerSession remote,Map<String, Object> data) { try{ System.out.println("Start Process Echo"); getBayeux().getChannel("/service/getlist").publish(getServerSession(), "Hello", null); System.out.println("End Process Echo"); }catch(Exception exp){ exp.printStackTrace(); } //remote.deliver(getServerSession(), "/service/getlist", data, null); } 

}

+4
source share
1 answer

Http://cometd.org has everything you need.

To create a very simple example (a web application with a Javascript client), you need to read, in particular:

  • This is for the client side.
  • This is for the server side (configuration)
  • This is for the server side (code). In this menu, you can start by using the first and third points: Inherited services for code that resonates with the entered message, and Integration of server services to configure the Bayeux server through the configuration servlet.

The pages I linked have all the necessary code, just copy and paste it. In case, come back with more specific questions.


edited

After looking at the code, I see that to configure the service you need to copy the code for the ConfigurationServlet class from here and for the EchoService class you need to change the processEcho method as follows:

 remote.deliver(getServerSession(), "/echo", data, null); 

with data is a HashMap, as described here (first example).

On the client side, I subscribed to the channel before posting your request (I'm not sure if it works in your way)

+4
source

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


All Articles