HTML and CSS Reference
In-Depth Information
this . server = new Server ();
Connector connector = new SelectChannelConnector ();
connector . setPort ( 8081 );
connector . setHost ( addr . getHostAddress ());
server . addConnector ( connector );
// 2) Register ChatWebSocketHandler in the
//Jetty server instance.
ChatWebSocketHandler chatWebSocketHandler =
new ChatWebSocketHandler ();
chatWebSocketHandler . setHandler ( new DefaultHandler ());
server . setHandler ( chatWebSocketHandler );
// 2) Start the Jetty server.
server . start ();
} catch ( Throwable e ) {
e . printStackTrace ();
}
}
....
}
Now we'll create a method to observe CDI events and send the fired Member events to
all active connections. This relays a very simple cdievent JavaScript object, which will
be pushed to all connected clients and then evaluated on the browser through a Java‐
Script interpreter.
public void observeItemEvent ( @Observes Member member ) {
try {
for ( ChatWebSocket webSocket : websockets ) {
webSocket . connection . sendMessage ( "{\"cdievent\":{\"fire\":function(){" +
"eventObj.initEvent(\'memberEvent\', true, true);" +
"eventObj.name = '" + member . getName () + "';\n" +
"document.dispatchEvent(eventObj);" +
"}}}" );
}
} catch ( IOException x ) {
//...
}
}
The above code observes the following event when a new Member is registered through
the web interface. As you can see below, memberEventSrc.fire(member) is fired when
a user registers through the provided RESTful URL.
@POST
@Consumes ( MediaType . APPLICATION_FORM_URLENCODED )
@Produces ( MediaType . APPLICATION_JSON )
public Response createMember ( @FormParam ( "name" ) String name ,
Search WWH ::




Custom Search