CometD を使用する Java Web アプリケーションがあります。ワークフローは簡単です。
- チャネル「/service/hello」でメッセージを受信したときに動作するサービスを定義しました。このサービスは、パラメーター「name」を想定しています。これに基づいて、次の名前のチャネルを作成します:
"/"+message.getDataAsMap().get("name")
. このチャネルには、すべてのサブスクライバーにメッセージを送り返すコールバック メソッドがアタッチされます。 - 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(); }
}
}
}