4

CometD を使用する Java Web アプリケーションがあります。ワークフローは簡単です。

  1. チャネル「/service/hello」でメッセージを受信したときに動作するサービスを定義しました。このサービスは、パラメーター「name」を想定しています。これに基づいて、次の名前のチャネルを作成します: "/"+message.getDataAsMap().get("name"). このチャネルには、すべてのサブスクライバーにメッセージを送り返すコールバック メソッドがアタッチされます。
  2. Javascript クライアント (dojo を使用) は、チャネル「/service/hello」にメッセージを発行し、「/service/hello」にパラメータとして送信された名前のチャネルにサブスクライブします。例を見てみましょう:
....
    cometd.subscribe('/1234', function(message)
    {
                    //do smth on message received;
     });

    cometd.publish('/service/hello', { name: '1234' });
....

これはうまくいきます。今、私が達成したいことは次のとおりです。Javascriptクライアントをサブスクライバーとしてのみ使用し、Javaクライアントをパブリッシュすることです。Java クライアント API の CometD2 ドキュメントに記載されている例を使用してこれを試しましたが、期待どおりに動作しません。サービスが呼び出されたようですが、Javascript コンシューマにはメッセージが表示されません。

これを達成することは可能ですか?何が間違っているかのアイデアはありますか?ありがとう。

サーバー側のコードは次のとおりです。

public class HelloService extends AbstractService {
    public HelloService(BayeuxServer bayeux)
    {
        super(bayeux, "hello");
        addService("/service/hello", "processHello");
    }

    public void processHello(ServerSession remote, Message message)
    {
        Map<String, Object> input = message.getDataAsMap();
        String name = (String)input.get("name");
        String channelId = "/"+name;
        addService(channelId, "processId");
        processId(remote, message);

    }

    public void processId(ServerSession remote, Message message)
    {
        Map<String, Object> input = message.getDataAsMap();
        String name = (String)input.get("name");
        int i = 0;
        Map<String, Object> output = new HashMap<String, Object>();
        while(i<1){
            i++;
            output.put("greeting", "Hello, " + name);
            remote.deliver(getServerSession(), "/"+name, output, null);
            try {
                Thread.sleep(1000);             } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();            }
        }
    } 
}
4

1 に答える 1

3

remote.deliver()remoteサービス チャネルで公開されたセッション (つまり、クライアント)に「回答」を送信します。

非請求メッセージを通常のチャネル (サーバー側にはまだ存在しない) に発行する必要があります。だから、あなたは次のように書くべきです

String channelName = "whatever - not beginning with /service";
getBayeux().createIfAbsent(channelName);
getBayeux().getChannel(channelName).publish(getServerSession(), output, null); 
于 2011-10-05T10:30:41.670 に答える