2

クライアントにメッセージを返信する際に問題が発生しています。以下は私のコードです

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

SomeClassFunction

class SomeClassFunction(){

}

ここでは、ブール変数のブール成功を作成しています。true の場合は、javascript のクライアントにメッセージを送信します。クライアントにメッセージを返す方法。私もこのラインを試しました。

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

しかし、リモートオブジェクトとgetServerSessionメソッドでエラーが発生しています。

4

1 に答える 1

3

目標を達成するために、リスナーを実装したり、チャネルを構成したりする必要はありません。たとえば、オーソライザーを追加するために、後の段階でいくつかの構成を追加する必要がある場合があります。

ConfigurationServletこれは、このリンクから取得した のコードです。

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から取得したクラスのコードです。

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);
    }
}
于 2011-12-15T17:50:37.183 に答える