2011-12-15 11 views
2

クライアントにメッセージを送信する際に問題があります。以下は、私のコードはここで私は、ブール変数に ブール成功を作成していCometDがクライアントにメッセージを公開します

は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メソッドでエラーが発生しています。

答えて

3

目的を達成するために、リスナーの実装やチャンネルの設定は必要ありません。たとえば、承認者を追加するために、後で設定を追加する必要があるかもしれません。

これは、このlinkから取ら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(); 
    } 
} 

これは、このlinkあちこちに取ら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); 
    } 
} 
+0

perissf以前と同様に、クライアントにメッセージを送り返すことに成功しています。しかし、私は上記の場合にしたい。エコークラスを使用する場合、someClassFunctionクラスの前に最初に呼び出すので、someClassFunctionの実行後にメッセージを送信したいので、私はそれを勉強しなければならないオーソライザの概念を教えてくれました。 –

+0

perissf、私はアイデアを持っている、私はそれを実装するとき私はあなたに知らせてくれます。 –

+0

もし私がperissfなら、もし私が特定のクラスから呼び出したいのであれば、configuredervletから新しいEcho(bayuex)を呼びたくないのですか? configurationServletの場合、web.xmlのon-loadの指定のためにコンストラクタを呼び出さなければなりません。私はクラスから呼び出す場合、私はこのようにしていますが、表示するメッセージはまだありません。 –

関連する問題