我想實現一個場景,當我需要通知特定客戶端時說A另一個客戶端B向服務器發出GET請求時。 我正在尋找Atmosphere for server push。 我已經成功地將B連接到服務器,並且能夠在通過大氣發送消息時推送他。但是這個特殊的場景我被卡住了。任何人都可以提出一個可行的解決方案嗎? 還有一些方向指針?服務器端從另一個客戶端獲取GET請求時推送到客戶端
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);
}
}
}
您可以發佈您的代碼嗎? – brandonscript
我會直接使用[WebSockets](http://en.wikipedia.org/wiki/WebSocket)。服務器端代碼會認識到它應該通知客戶端,並且它將使用WebSocket來執行此操作。檢查[this](http://www.html5rocks.com/en/tutorials/websockets/basics/)後,我想你會看到一種方法來實現這種溝通。 希望我幫助! –
上面的代碼實際上也處理websocket請求。我真正的問題是使HTTP-GET調用的客戶端實際上向先前註冊的客戶端發起了PUSH請求。我們如何映射客戶端A - > HTTP/GET --->服務器---> Socket Push - >客戶端B –