With Netty’s new 4.x API, there have been some changes (some big some small). I have migrated an old server core of mine to work with the new API, but I cannot figure out a way to solve this problem. In 3.x, I could use
MessageEvent e
This allowed me to cast e to the type I needed (in this case, ServerMessage and String, as those were the two types used for server->client messages).
When using 3.x, I could do:
public void writeRequested(ChannelHandlerContext chctx, MessageEvent e)
{
if(e.getMessage() instanceof ServerMessage) //change
{
ServerMessage message = (ServerMessage) e.getMessage();
Channels.write(chctx,e.getFuture(),message.getData());
logger.debug("Message sent (id: "+message.getMessageID()+ " data: "+message.getMessageBody()+")");
}
else if(e.getMessage() instanceof String)
{
String data =(String)e.getMessage();
ChannelBuffer buffer = ChannelBuffers.buffer(data.length());
buffer.writeBytes(data.getBytes());
Channels.write(chctx,e.getFuture(),buffer);
logger.debug("Written string (possible <policy-file-request />) to client #id ->" +
+Environment.getGameInstance().getManager().getSession(chctx.getChannel());
}
}
However, with 4.x, I am a bit confused to what I can use. I have partially implemented simple sending like so;
public void encode(ChannelHandlerContext ctx, ServerMessage msg, ByteBuf buffer) {
if(msg instanceof ServerMessage) {
ServerMessage message = (ServerMessage)msg;
ctx.channel().write(message.getData());
logger.debug("Message sent (id: "+message.getMessageID()+ " data: "+message.getMessageBody()+")");
}
else {
// string stuff here
}
}
My basic problem is that how would I go about writing a String as well as a ServerMesssage to the client?
(My String is a simple )
Any help is greatly appreciated!
Just use the common super-class of the two messages and then cast as you did in 3.x if you really want to use the same ChannelHandler for both Objects. Anyway I think it would be much more clean if you just use two different ChannelHandler.