我正在尝试实现一个场景,当我需要通知特定客户端时,当另一个客户端B向服务器发出GET请求时说出A. 我正在寻找Atmosphere服务器推送。 我已成功将B连接到服务器,并且如果通过Atmosphere发送消息则能够推送他。但是这个特殊情况我被卡住了。任何人都可以提出可行的解决方案吗? 还有一些方向指示?
import org.atmosphere.annotation.Broadcast;
import org.atmosphere.annotation.Suspend;
import org.atmosphere.config.service.AtmosphereService;
import org.atmosphere.cpr.AtmosphereResourceEvent;
import org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter;
import org.atmosphere.jersey.JerseyBroadcaster;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
/**
* Simple chat resource demonstrating the power of Atmosphere. This resource supports transport like WebSocket, Streaming, JSONP and Long-Polling.
*
* @author Jeanfrancois Arcand
*/
@Path("/")
@AtmosphereService (broadcaster = JerseyBroadcaster.class)
public class ChatResource {
/**
* Suspend the response without writing anything back to the client.
*
* @return a white space
*/
@Suspend(contentType = "application/json", listeners = {OnDisconnect.class})
@GET
public String suspend() {
return "";
}
/**
* Broadcast the received message object to all suspended response. Do not write back the message to the calling connection.
*
* @param message a {@link Message}
* @return a {@link Response}
*/
@Broadcast(writeEntity = false)
@POST
@Produces("application/json")
public Response broadcast(Message message) {
return new Response(message.author, message.message);
}
public static final class OnDisconnect extends AtmosphereResourceEventListenerAdapter {
/**
* {@inheritDoc}
*/
@Override
public void onDisconnect(AtmosphereResourceEvent event) {
System.out.println(event);
}
}
}
答案 0 :(得分:2)
不使用@Broadcast,而是直接使用BroadcasterFactory使用Broadcaster,并调用broadcaster.broadcast(message,ClientB)。看看atmosphere-sample's,有很多例子可以做到这一点。
- Jeanfrancois
答案 1 :(得分:0)
使用BroadcasterFactory直接使用broadcaster并调用broadcaster.broadcast(message,ClientB)