我正在学习tyrus WebSocket的实现。但我不明白为什么以及何时在websocket端点中需要多个编码器或解码器。例如:
@ServerEndpoint(value = "/subscribe", decoders = { TextStreamMessageDecoder.class }, encoders = { TextStreamMessageEncoder.class })
public class ChatServerEndPoint {
......
}
解码器和编码器列表中只有一个解码器和编码器。由于这些是解码器或编码器阵列,因此我可以一次使用多种类型的编码器或解码器。但是在后面提到的api描述中,
如果它总是使用列表的第一个元素,那么WebSockets API中的多个编码器或解码器的用例是什么?
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());
}
}
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();
}
try {
Gson gson = new Gson();
session.getBasicRemote().sendObject(
gson.toJson(new ClientMessage(1, "FirstName")));
} catch (EncodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
客户端使用MessageEncoder类发送消息,而Server有两个编码器和解码器,包括TextStreamMessageEncoder或解码器和MessageEncoder或解码器。那么客户端向服务器发送消息时会使用哪个解码器?
答案 0 :(得分:1)
解码器确实有#willDecode method,所以你可以有一个解码json,另一个用于解码hocon等。
广告编码器 - 它们是根据通用类型选择的 - 运行时应始终优先选择“最接近的”,基于您尝试发送(编码)的类型。
此外,您可以拥有多个onMessage方法类型 - text / binary,因此您可能需要注册测试和二进制解码器。
答案 1 :(得分:0)
根据第一个答案,我想将其概括为风箱:
让我们创建两种类型的编码器和解码器,包括Text和BinaryStream。
public class JsonMessageEncoder implements Encoder.Text<JsonWrapper> {
public String encode(JsonWrapper jsonWrapper) throws EncodeException {
return jsonWrapper.getJson().toString();
}
}
public class JsonMessageDecoder implements Decoder.Text<JsonWrapper> {
public JsonWrapper decode(String s) throws DecodeException {
JsonObject json = Json.createReader(new StringReader(s)).readObject();
System.out.println("JsonWrapper decode: "+json);
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 BinaryStreamMessageEncoder implements Encoder.BinaryStream<BinaryMessage>{
public void encode(BinaryMessage object, OutputStream os)
throws EncodeException, IOException {
ObjectOutput objectOutput = new ObjectOutputStream(os);
objectOutput.writeObject(object);
}
}
public class BinaryStreamMessageDecoder implements Decoder.BinaryStream<BinaryMessage>{
public BinaryMessage decode(InputStream is) throws DecodeException,
IOException {
ObjectInput objectInput = new ObjectInputStream(is);
try {
BinaryMessage binaryMessage= (BinaryMessage) objectInput.readObject();
return binaryMessage;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
现在在客户端和服务器端点中添加这些编码器和解码器。
@ClientEndpoint(decoders = { JsonMessageDecoder.class, BinaryStreamMessageDecoder.class}, encoders = { JsonMessageEncoder.class, BinaryStreamMessageEncoder.class })
public class ChatClientEndPoint {
@OnMessage
public void onMessage(JsonWrapper message, Session session) {
logger.info("JsonWrapper onMessage: " + session.getId());
if (this.messageHandler != null)
this.messageHandler.handleMessage(message);
}
@OnMessage
public void onMessage(BinaryMessage message, Session session) {
logger.info("BinaryMessage onMessage: " + session.getId());
if (this.messageHandler != null)
this.messageHandler.handleMessage(message);
}
}
@ServerEndpoint(value = "/subscribe", decoders = { JsonMessageDecoder.class, BinaryStreamMessageDecoder.class}, encoders = { JsonMessageEncoder.class,BinaryStreamMessageEncoder.class })
public class ChatServerEndPoint {
@OnMessage
public void onMessage(JsonWrapper message, Session session) {
logger.info("JsonWrapper onMessage: " + session.getId());
// logger.info("onMessage: " + message.toString());
Gson gson = new Gson();
ClientMessage clientMessage = gson.fromJson(message.getJson().toString(),
ClientMessage.class);
System.out.println(clientMessage.toString());
}
@OnMessage
public void onMessage(BinaryMessage message, Session session) {
logger.info("BinaryMessage onMessage: " + session.getId());
System.out.println(message.toString());
}
}
try {
session.getBasicRemote().sendObject(new BinaryMessage("Binary Message"));
} catch (EncodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
session.getBasicRemote().sendObject(
gson.toJson(new ServerMessage(1, "connection accepted")));
} catch (EncodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
在示例中:我们对每个端点使用两个@OnMessage方法,其中一个用于Text类型,另一个用于BinaryStream类型解码器。客户端和服务器相互发送不同类型的消息,其中客户端发送BinaryMessage,当服务器发送Json对象时,BinaryMessage具有一个String属性。因此,运行时将检测通信时将使用哪种类型的解码器和编码器。
JsonWrapper decode:{“id”:1,“message”:“接受连接”}
ServerMessage [id = 1,message = connection accepted]
BinaryMessage [name =二进制消息]