I use netty and I want to send various objects from client to server and vice versa.
I created same decoders and encoders classes on client and server ;
Decoder:
public class UserInfoDecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
...
list.add(new UserInfo(...))
}
}
Encoder:
public class UserInfoEncoder extends MessageToByteEncoder<UserInfo> {
@Override
protected void encode(ChannelHandlerContext ctx, UserInfo msg, ByteBuf out) {
...
out.writeBytes(...);
}
}
Here is my server initChannel method:
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
if (sslCtx != null) {
p.addLast(sslCtx.newHandler(ch.alloc()));
}
p.addLast(
// new ObjectEncoder(),
// new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
new UserInfoDecoder(),
new UserInfoEncoder(),
new ObjectEchoServerHandler());
}
There is a method channelRead in server handler class
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
...
}
How to differ objects what was sent by client?
For e.g. now I have only UserInfo class and i can cast Object msg in channelRead to UserInfo, but i want to send UsersCar object too for e.g., how to differ objects by types which were sent?