I am learning tyrus WebSocket implementation. But I don't understand why and when do we need more then one encoders or decoders in the websocket endpoints. For example:
@ServerEndpoint(value = "/subscribe", decoders = { TextStreamMessageDecoder.class }, encoders = { TextStreamMessageEncoder.class })
public class ChatServerEndPoint {
......
}
There is only one decoder and encoder in the decoders and encoders list. As these are array of decoders or encoders then I can use multiple types of encoders or decoders at a time. But in the api description then mentioned,
The websocket runtime will use the first decoder in the list able to decode a message, ignoring the remaining decoders.
If it always use the first elements of the list then what are the usecases of multiple encoder or decoder in the WebSockets API?
After Editing
public class TextStreamMessageDecoder implements Decoder.TextStream<JsonWrapper>{
public JsonWrapper decode(Reader reader) throws DecodeException,
IOException {
JsonReader jsonReader = Json.createReader(reader);
JsonObject jsonObject = jsonReader.readObject();
return new JsonWrapper(jsonObject);
}
}
public class TextStreamMessageEncoder implements Encoder.TextStream<JsonWrapper>{
public void encode(JsonWrapper object, Writer writer)
throws EncodeException, IOException {
JsonWriter jsonWriter = Json.createWriter(writer);
JsonObject jsonObject = object.getJson();
jsonWriter.write(jsonObject);
}
}
public class MessageDecoder implements Decoder.Text<JsonWrapper> {
public JsonWrapper decode(String s) throws DecodeException {
JsonObject json = Json.createReader(new StringReader(s)).readObject();
return new JsonWrapper(json);
}
public boolean willDecode(String s) {
// TODO Auto-generated method stub
try {
Json.createReader(new StringReader(s)).read();
return true;
} catch (JsonException ex) {
ex.printStackTrace();
return false;
}
}
}
public class MessageEncoder implements Encoder.Text<JsonWrapper> {
public String encode(JsonWrapper jsonWrapper) throws EncodeException {
return jsonWrapper.getJson().toString();
}
}
@ClientEndpoint(decoders = { MessageDecoder.class}, encoders = { MessageEncoder.class })
public class ChatClientEndPoint {
@OnMessage
public void onMessage(String message, Session session) {
logger.info("onMessage: " + session.getId());
if (this.messageHandler != null)
this.messageHandler.handleMessage(message);
}
}
@ServerEndpoint(value = "/subscribe", decoders = { TextStreamMessageDecoder.class, MessageDecoder.class}, encoders = { TextStreamMessageEncoder.class, MessageEncoder.class })
public class ChatServerEndPoint {
@OnMessage
public void onMessage(String message, Session session) {
logger.info("onMessage: " + session.getId());
// logger.info("onMessage: " + message.toString());
Gson gson = new Gson();
ClientMessage clientMessage = gson.fromJson(message,
ClientMessage.class);
System.out.println(clientMessage.toString());
}
}
Server sends to Client:
try {
Gson gson = new Gson();
session.getBasicRemote().sendObject(
gson.toJson(new ServerMessage(1, "connection accepted")));
} catch (EncodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Clients sends to Server:
try {
Gson gson = new Gson();
session.getBasicRemote().sendObject(
gson.toJson(new ClientMessage(1, "FirstName")));
} catch (EncodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Clients sends message using MessageEncoder Class while Server has two encoders and decoder including TextStreamMessageEncoder or decoder and MessageEncoder or decoder. So which decoder will be used when client send message to server?