2014-07-02 21 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