|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Grizzlet
This interface defines the contract between the Grizzlet Container (GrizzlyComet). Implementation of this interface will enable the Grizzly Comet Web Server (or Grizzlet Container) to send data to its subcribed clients without having any need for the client to request for it. It allows creation of event-driven web applications which are hosted in the browser. This interface is the only component needed by an application that want to implement support for Asynchronous Request Processing or Comet Request Processing. For example, an AJAX based Chat application will only have to define the following
public void onRequest(AsyncConnection event) throws IOException {
GrizzletRequest req = event.getRequest();
GrizzletResponse res = event.getResponse();
res.setContentType("text/html");
res.addHeader("Cache-Control", "private");
res.addHeader("Pragma", "no-cache");
if (event.isGet()){
res.write("\n");
res.flush();
event.suspend();
return;
} else if (event.isPost()){
res.setCharacterEncoding("UTF-8");
String action = req.getParameterValues("action")[0];
String name = req.getParameterValues("name")[0];
if ("login".equals(action)) {
event.push(
BEGIN_SCRIPT_TAG + toJsonp("System Message", name
+ " has joined.") + END_SCRIPT_TAG);
res.write("success");
res.flush();
} else if ("post".equals(action)) {
String message = req.getParameterValues("message")[0];
event.push(BEGIN_SCRIPT_TAG
+ toJsonp(name, message) + END_SCRIPT_TAG);
res.write("success");
res.flush();
} else {
res.setStatus(422);
res.setMessage("Unprocessable Entity");
res.write("success");
res.flush();
}
}
}
public void onPush(AsyncConnection event) throws IOException{
GrizzletRequest req = event.getRequest();
GrizzletResponse res = event.getResponse();
// The connection will be closed, flush the last String.
if (event.isResuming()){
String script = BEGIN_SCRIPT_TAG
+ "window.parent.app.listen();\n"
+ END_SCRIPT_TAG;
res.write(script);
res.flush();
res.finish();
return;
} else if (event.hasPushEvent()){
// The Chatroom has been updated, push the data to our
// subscribed clients.
res.write(event.getPushEvent().toString());
res.flush();
return;
}
}
Method Summary | |
---|---|
void |
onPush(AsyncConnection asyncConnection)
This method is invoked when the Grizzlet Container execute a push operations. |
void |
onRequest(AsyncConnection asyncConnection)
When a client send a request to its associated Grizzlet, it can decide if the underlying connection can be suspended (creating a Continuation) or handle the connection synchronously. |
Method Detail |
---|
void onRequest(AsyncConnection asyncConnection) throws IOException
asyncConnection
- An object representing an asynchronous connection.
IOException
void onPush(AsyncConnection asyncConnection) throws IOException
asyncConnection
- An object representing an asynchronous connection.
IOException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |