2012-09-06 3 views
0

서버에서 클라이언트로 메시지를 푸시하려고합니다. 나는 DOJO 1.7, Cometd 및 Jetty를 tomcat6과 통합하여 사용하고 있습니다.클라이언트 (도장)에 COMETD를 사용하는 서버 푸시

//Server side code 
    public class notificationService extends AbstractService { 

public notificationService(BayeuxServer bayeux, String name) { 
    super(bayeux, name); 
    System.out.println("Inside constrcutor of Notification Service"); 
    addService("/notification", "processNotification"); 
} 


    public void processNotification(ServerSession remote,ServerMessage.Mutable message)  
     { 
    System.out.println("Inside process Notification"); 
    Map<String,Object> response = new HashMap<String,Object>(); 
    response.put("payload",new java.util.Date()); 
      getBayeux().createIfAbsent("/notification"); 
      getBayeux().getChannel("/notification").publish(getServerSession(),response,null); 
      //remote.deliver(getServerSession(),"/notification", response, null); 
     } 

     //Client Side Code (DOJO) 

     var cometd = dojox.cometd; 
     cometd.init("http://serverip:port/cometd") 
     cometd.publish('/notification',{ mydata: { foo: 'bar' } }); 
     cometd.subscribe('/notification', function(message) 
      { 
        //alert("Message received" + message.data.payload); 
        //alert(message.data.payload); 
        alert("Message received"); 
      }); 

특정 채널을 구독하는 모든 클라이언트에게 메시지를 브로드 캐스트하려고합니다. remore.deliver를 사용하면 개별 클라이언트에 메시지를 보내지 만 해당 채널에 가입 한 모든 클라이언트에게는 메시지를 보내지 않습니다. channel.publish가 나를 위해 작동하지 않습니다 ... 어떤 도움이나 의견을 보내 주시면 감사하겠습니다.

답변

2

클라이언트가 메시지를 서비스에서 수신 할뿐만 아니라 해당 채널의 모든 구독자에게 브로드 캐스트하도록 브로드 캐스트 채널 인 정의 채널 을 게시합니다.

그런 다음 서비스에 당신은 /notification 채널의 가입자에게 다른 메시지를 보낼 것 ServerChannel.publish() 전화 (서비스 자체 떨어져 - 무한 루프 방지가있다).

이러한 종류의 작업을 수행하는 더 좋은 방법은 서비스 채널 (예 : /service/notification)을 사용하여 클라이언트에서 서비스로 초기 메시지를 보내는 것입니다. 그러면 서비스는 지금처럼 채널 /notification으로 브로드 캐스팅 할 수 있습니다.

ServerChannel.publish()을 호출하면 서비스에서 메시지를 브로드 캐스트하는 올바른 방법입니다. 불행히도 당신은 왜 당신을 위해 일하지 않는지 정확히 지정하지 않으므로 도와 드릴 수 없습니다.

먼저 클라이언트와 서비스 사이의 첫 번째 메시지 서비스 채널을 사용하여 시작할 것입니다.

Tomcat 6은 비동기 서블릿 (Servlet 3)을 지원하지 않기 때문에 실제로 CometD를위한 최상의 솔루션이 아닙니다.

Jetty 8과 같은 Servlet 3 호환 컨테이너를 사용하는 것이 좋습니다.

관련 문제