Reputation: 754
I am trying to implement a scenario when I need to notify a specific client say A when another client B makes a GET request to the server. I am looking at Atmosphere for server push. I have successfully connected B to the server and able to Push him if sending messages through Atmosphere. But this particular scenario I am stuck. Can any one please present a viable solution? Else some pointers in the direction?
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);
}
}
}
Upvotes: 0
Views: 1589
Reputation: 4207
Use broadcaster directly by using BroadcasterFactory and invoke broadcaster.broadcast(message,ClientB)
Upvotes: 0
Reputation: 1705
Instead of using @Broadcast, use Broadcaster directly using the BroadcasterFactory, and invoke broadcaster.broadcast(message, ClientB). Look at atmosphere-sample's, there is many example doing that.
-- Jeanfrancois
Upvotes: 2