2014-07-02 59 views
0

我試着開發一個小的gwt聊天應用程序Atmosphere的gwt擴展。應用程序的服務器端部分注意傳入的AtmosphereResource關聯廣播器並暫停它。 這裏是關於暫停新的傳入AtmosphereResource代碼片段:AtmosphereResource變爲空

private void doGet(AtmosphereResource ar, String userId) { 
    if(BroadcasterFactory.getDefault().lookup(userId) != null) { 
    ar.setBroadcaster(BroadcasterFactory.getDefault().lookup(userId).addAtmosphereResource(ar)); 
    } else { 
      ar.setBroadcaster(BroadcasterFactory.getDefault().get(userId).addAtmosphereResource(ar)); 
    } 
    ar.suspend(); 
    ... 
} 

當我尋找那些AtmosphereResource,使用它以前存儲的UUID,我一直覺得很空:

... 
AtmosphereResource arTarget = AtmosphereResourceFactory.getDefault().find(uuid);    
if (arTarget != null) { 
    arTarget.getBroadcaster().broadcast(msg,arTarget); 
} else { 
    log.info("handleRawMessage:no broadcaster "+((RawMessage) msg).toString()); 
} 
... 

有什麼不對?我還注意到,AtmosphereResources在立即超時時迅速獲得了onResume,但超時設置爲-1。我錯過了什麼?這是我pom的一個片段。

<dependency> 
    <groupId>org.atmosphere.extensions</groupId> 
    <artifactId>atmosphere-gwt20-client</artifactId> 
    <version>2.1.1</version> 
</dependency> 
<dependency> 
    <groupId>org.atmosphere.extensions</groupId> 
    <artifactId>atmosphere-gwt20-common</artifactId> 
    <version>2.1.1</version> 
</dependency> 
<dependency> 
    <groupId>org.atmosphere.extensions</groupId> 
    <artifactId>atmosphere-gwt20-server</artifactId> 
    <version>2.1.1</version> 
</dependency> 
<dependency> 
    <groupId>org.atmosphere</groupId> 
    <artifactId>atmosphere-runtime</artifactId> 
    <version>2.1.1</version> 
</dependency> 

回答

0

爲了得到一個廣播公司,你必須有一個客戶端連接到服務器。你確定你的客戶正在連接嗎?

版本2.0

爲了實現我曾經做過在2.0服務器:(客戶端ID可以從請求獲得)

public class AtmosphereHandler extends AbstractReflectorAtmosphereHandler { 

private static Logger logger = Logger.getLogger(AtmosphereHandler.class);; 

@Override 
public void onRequest(final AtmosphereResource ar) throws IOException { 
    logger.info("Connecting to comet with: " 
      + ar.getRequest().getRequestURI()); 
    Broadcaster broadcaster = BroadcasterFactory.getDefault().lookup(
      YOUR_CLIENT_ID, true); 
    if (broadcaster.getAtmosphereResources().size() > 0) { 
     logger.debug("Broadcaster recovered with name: " 
       + broadcaster.getID()); 
    } else { 
     logger.debug("Broadcaster created with name: " 
       + broadcaster.getID()); 
    } 
    ar.setBroadcaster(broadcaster); 

    ar.setSerializer(new Serializer() { 
     Charset charset = Charset.forName(ar.getResponse() 
       .getCharacterEncoding()); 

     @Override 
     public void write(OutputStream os, Object o) throws IOException { 
      try { 
       logger.info("Writing object to JSON outputstream with charset: " 
         + charset.displayName()); 
       String payload = serializer.serialize(o); 
       os.write(payload.getBytes(charset)); 
       os.flush(); 
      } catch (SerializationException ex) { 
       throw new IOException("Failed to serialize object to JSON", 
         ex); 
      } 
     } 
    }); 

    ar.suspend(); 

} 

private ServerSerializer serializer = new JacksonSerializerProvider() 
     .getServerSerializer(); 

在客戶端部分,你應該有一些類似於:

 AutoBeanClientSerializer json_serializer = new AutoBeanClientSerializer(); 
    json_serializer.registerBeanFactory(beanFactory, ActivityMessage.class); 
    AtmosphereRequestConfig jsonRequestConfig = AtmosphereRequestConfig.create(json_serializer); 
    jsonRequestConfig.setUrl(GWT.getHostPageBaseURL() + HANDLER_URL_PART); 
    jsonRequestConfig.setContentType("application/json; charset=UTF-8"); 
    jsonRequestConfig.setTransport(AtmosphereRequestConfig.Transport.WEBSOCKET); 
    jsonRequestConfig.setFallbackTransport(AtmosphereRequestConfig.Transport.STREAMING); 
    jsonRequestConfig.setOpenHandler(new AtmosphereOpenHandler() { 
     @Override 
     public void onOpen(AtmosphereResponse response) { 
      GWT.log("JSON Connection opened"); 
     } 
    }); 
    jsonRequestConfig.setCloseHandler(new AtmosphereCloseHandler() { 
     @Override 
     public void onClose(AtmosphereResponse response) { 
      GWT.log("JSON Connection closed"); 
     } 
    }); 
    jsonRequestConfig.setMessageHandler(new AtmosphereMessageHandler() { 
     @Override 
     public void onMessage(AtmosphereResponse response) { 

     } 
    }); 


    Atmosphere atmosphere = Atmosphere.create(); 
    final AtmosphereRequest jsonRequest = atmosphere.subscribe(jsonRequestConfig); 

VERSION 1.1.0RC4

我延長AtmosphereGwtHandler實現這樣的服務器:

private static Logger logger; 
@Override 
public void init(ServletConfig servletConfig) throws ServletException { 
    super.init(servletConfig); 
    logger = Logger.getLogger(AtmosphereHandler.class); 
} 

@Override 
public int doComet(GwtAtmosphereResource resource) throws ServletException, IOException { 
    logger.info("Connecting to comet with: " +resource.getRequest().getRequestURI()); 
    Broadcaster broadcaster = BroadcasterFactory.getDefault().lookup(YOUR_CONNECTION_ID, true); 
    if(broadcaster.getAtmosphereResources().size()>0){ 
     logger.debug("Broadcaster recovered with name: " + broadcaster.getID()); 
    } 
    else{ 
     logger.debug("Broadcaster created with name: " + broadcaster.getID()); 
    } 
    resource.getAtmosphereResource().setBroadcaster(broadcaster); 
    return NO_TIMEOUT; 
} 

@Override 
public void cometTerminated(GwtAtmosphereResource cometResponse, boolean serverInitiated) { 
    logger.info("Disconnecting from comet. Broadcaster : " + cometResponse.getBroadcaster().getID()); 
    super.cometTerminated(cometResponse, serverInitiated); 
} 

@Override 
public void doPost(HttpServletRequest postRequest, HttpServletResponse postResponse, 
     List<?> messages, GwtAtmosphereResource cometResource) { 
    broadcast(messages, cometResource); 
} 

你必須在你的目標目錄的META-INF文件夾中的文件atmosphere.xml進行初始化。

我希望有幫助!

+0

您好!感謝您的回覆。爲什麼你爲服務器端實現了AtmosphereGWTHandler的擴展?我使用AbstractReflectorAtmosphereHandler實現我的服務器端。您是否將這個擴展名作爲氛圍處理程序指向您的atmosphere.xml文件? – Francesco

+1

是的,我使用舊版本,我意識到之後,爲什麼我編輯我的答案,把我使用的氣氛版本。現在我正在嘗試2.0。我現在告訴你,如果它也在工作;) – Kasas

+0

我已經測試了氣氛2.0,我可以找到沒有問題的廣播公司。你確定你有一個客戶端連接?我會發布我的代碼。 – Kasas