|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
public interface AtmosphereHandler
Implementation of AtmosphereHandler allows creation of
event-driven web applications which are hosted in the browser.
An AtmosphereHandler allows a web application to suspend and resume
an http response. An http response can be suspended AtmosphereResource.suspend()
and later resume via AtmosphereResource.resume(). Messages can also
be shared between suspended response and their associated AtmosphereHandler
using a Broadcaster. Any invocation of Broadcaster.broadcast(java.lang.Object)
will allow a suspended response to write the content of the message
onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent).
AtmosphereHandler must be thread safe
For example, a simple pubsub based AtmosphereHandler will take the form of
public class AtmosphereHandlerPubSub extends AbstractReflectorAtmosphereHandler {
public void onRequest(AtmosphereResource r) throws IOException {
AtmosphereRequest req = r.getRequest();
AtmosphereResponse res = r.getResponse();
String method = req.getMethod();
// Suspend the response.
if ("GET".equalsIgnoreCase(method)) {
// Log all events on the console, including WebSocket events.
r.addEventListener(new WebSocketEventListenerAdapter());
res.setContentType("text/html;charset=ISO-8859-1");
Broadcaster b = lookupBroadcaster(req.getPathInfo());
r.setBroadcaster(b);
if (req.getHeader(HeaderConfig.X_ATMOSPHERE_TRANSPORT).equalsIgnoreCase(HeaderConfig.LONG_POLLING_TRANSPORT)) {
req.setAttribute(ApplicationConfig.RESUME_ON_BROADCAST, Boolean.TRUE);
r.suspend(-1, false);
} else {
r.suspend(-1);
}
} else if ("POST".equalsIgnoreCase(method)) {
Broadcaster b = lookupBroadcaster(req.getPathInfo());
String message = req.getReader().readLine();
if (message != null && message.indexOf("message") != -1) {
b.broadcast(message.substring("message=".length()));
}
}
}
public void destroy() {
}
Broadcaster lookupBroadcaster(String pathInfo) {
String[] decodedPath = pathInfo.split("/");
Broadcaster b = BroadcasterFactory.getDefault().lookup(decodedPath[decodedPath.length - 1], true);
return b;
}
}
| Method Summary | |
|---|---|
void |
destroy()
Destroy this handler |
void |
onRequest(AtmosphereResource resource)
When a client send a request to its associated AtmosphereHandler, it can decide
if the underlying connection can be suspended (creating a Continuation)
or handle the connection synchronously. |
void |
onStateChange(AtmosphereResourceEvent event)
This method is invoked when the Broadcaster execute a broadcast
operations. |
| Method Detail |
|---|
void onRequest(AtmosphereResource resource)
throws IOException
AtmosphereHandler, it can decide
if the underlying connection can be suspended (creating a Continuation)
or handle the connection synchronously.
It is recommended to only suspend request for which HTTP method is a GET
and use the POST method to send data to the server, without marking the
connection as asynchronous.
resource - an AtmosphereResource
IOException
void onStateChange(AtmosphereResourceEvent event)
throws IOException
Broadcaster execute a broadcast
operations. When this method is invoked its associated Broadcaster, any
suspended connection will be allowed to write the data back to its
associated clients. AtmosphereResource.resume() gets invoked. In that case,
AtmosphereResourceEvent.isResuming() will return true.AtmosphereResource.suspend(long)
expires. In that case, AtmosphereResourceEvent.isResumedOnTimeout() will return
true
event - an AtmosphereResourceEvent
IOExceptionvoid destroy()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||