Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the usecases of encoder list or decoder list in the WebSocket EndPoints annotation?

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?

like image 836
Md. Mahedi Kaysar Avatar asked Jul 14 '15 12:07

Md. Mahedi Kaysar


1 Answers

Decoders do have #willDecode method, so you could have one decoding json, other one for decoding hocon etc.

Ad encoder - they are chosen based on the generic type - runtime should always prefer the "closest" one, based on the type you are trying to send (encode).

Also, you can have more than one onMessage method type - text/binary, so you might want to register test and binary decoders.

like image 112
Pavel Bucek Avatar answered Nov 05 '22 01:11

Pavel Bucek