Java Reference
In-Depth Information
To encode a string-based message into an object of type AcmeChatObj , an encoder can be registered with a
WebSocket endpoint by specifying a value for the @ServerEndpoint annotation encoders attribute. In this example,
an encoder named AcmeChatEncoder will be used to encode string-based messages into objects of type AcmeChatObj .
The example code is as follows:
public class AcmeChatTextEncoder implements javax.websocket.Encoder.Text<AcmeChatObj> {
@Override
public String encode(AcmeChatObj object) throws EncodeException {
return object.getMessage();
}
@Override
public void init(EndpointConfig config) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void destroy() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Similarly, a decoder class can be implementd to convert the AcmeChatObj back into a string. To register a decoder
with a WebSocket endpoint, specify a value for the @ServerEndpoint annotation decoders attribute. In this example,
a decoder named AcmeChatDecoder will be used to decode an AcmeChatObj into a String. The example code is as follows:
public class AcmeChatTextDecoder implements javax.websocket.Decoder.Text<AcmeChatObj> {
@Override
public AcmeChatObj decode(String s) throws DecodeException {
AcmeChatObj obj = new AcmeChatObj();
obj.setMessage(s);
return obj;
}
@Override
public boolean willDecode(String s) {
if (s != null) {
return true;
} else {
return false;
}
}
@Override
public void init(EndpointConfig config) {
throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates.
}
Search WWH ::




Custom Search