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


i learning tyrus websocket implementation. don't understand why , when need more 1 encoders or decoders in websocket endpoints. example:

@serverendpoint(value = "/subscribe", decoders = { textstreammessagedecoder.class }, encoders = { textstreammessageencoder.class }) public class chatserverendpoint { ...... } 

there 1 decoder , encoder in decoders , encoders list. these array of decoders or encoders can use multiple types of encoders or decoders @ time. in api description mentioned,

the websocket runtime use first decoder in list able decode message, ignoring remaining decoders.

if use first elements of list usecases of multiple encoder or decoder in 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 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 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 2 encoders , decoder including textstreammessageencoder or decoder , messageencoder or decoder. decoder used when client send message server?

decoders have #willdecode method, have 1 decoding json, other 1 decoding hocon etc.

ad encoder - chosen based on generic type - runtime should prefer "closest" one, based on type trying send (encode).

also, can have more 1 onmessage method type - text/binary, might want register test , binary decoders.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -