仅使用Atmosphere向一个客户端广播

时间:2013-04-22 21:24:18

标签: java atmosphere

如何使用Atmosphere(Meteor)从一个客户端向另一个客户端广播消息?我目前基于流星教程实现此实现

@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
    Meteor.build(req).addListener(new AtmosphereResourceEventListenerAdapter());
}

@Override
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
    String body = req.getReader().readLine().trim();
    //some DAO lookups - here I would like to say I want to broadcast only to concrete client
    BroadcasterFactory.getDefault().lookup(DefaultBroadcaster.class, "/*").broadcast(UserDAO.getInstance().getUser(name));
}

3 个答案:

答案 0 :(得分:3)

答案 1 :(得分:3)

我相信另一个解决方案:对于只处理一个客户端,您不需要广播,您可以这样做:

     try
     {
        r.getResponse().write(message);
     }
     catch(IllegalStateException e)
     {
        logger.error("Could not send message through atmosphere " + userId);
     }

其中r是您可以在程序中记忆的资源。

答案 2 :(得分:0)

BroadcasterFactory.getDefault().lookup(atmosphereResource.uuid()).broadcast('something');