Java Reference
In-Depth Information
<input id="wsRelay" type="button" value="Send Message"
onclick="acmeChatRelay();"/>
</h:form>
</ui:define>
</ui:composition>
</h:body>
...
When the button is pressed, the message will be sent from the browser client to the WebSocket endpoint, and
a message will be returned from the endpoint to the client. Alerts will be displayed as the asynchronous events occur.
Message Types
As noted before, WebSockets can be very useful for sending different types of messages. In fact, just about any Java
object can be sent as a WebSocket message. Classes known as encoders and decoders can be utilized to facilitate
different message types. A message can be encoded in to any Java object type by implementing a custom encoder
class, and similarly, an object can be decoded back into a message by implementing a custom decoder class. Encoders
and Decoders can be created for the processing of the following types of messages:
Text Messages
Binary Messages
Pong Messages (Messages containing metadata regarding the WebSocket connection)
Let's take a look at a simple example to demonstrate how to encode a String based message into a Java object,
and then decode it back into a message later. The class below, AjaxChatObj , is a simple Java object that can be used to
hold String based messages.
public class AcmeChatObj {
private String message;
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @param message the message to set
*/
public void setMessage(String message) {
this.message = message;
}
}
 
Search WWH ::




Custom Search