-3

all i will write chatroom use netty for server. and flash for client. The protocol use json string with "\r\n" end.I write three handler "MessageDecoder","MessageHandler","MessageEncoder". and use flash client send a message. but i get following errors.

 java.lang.IllegalStateException: decode() method must read at least one byte if it returned a frame (caused by: class com.mbaobao.chatroom.socket.handlers.MessageDecoder)
at org.jboss.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:294)
at org.jboss.netty.handler.codec.frame.FrameDecoder.messageReceived(FrameDecoder.java:216)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:274)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:261)
at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:351)
at org.jboss.netty.channel.socket.nio.NioWorker.processSelectedKeys(NioWorker.java:282)
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:202)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)

MessageDecoder

    protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer)
                                                                                            throws Exception {
    logger.debug("message decode");
    if (buffer.readableBytes() < 4) {
        return null;
    }

    StringBuffer stringBuffer = new StringBuffer();
    String json = null;
    for (int i = 0; i < buffer.capacity(); i++) {
        char c = (char) buffer.getByte(i);
        logger.info(c);
        stringBuffer.append(c);
        if (c == 13 || c == 10) {
            json = stringBuffer.toString();
            break;
        }
    }
    ChatData chatData = JSON.parseObject(json, ChatData.class);

    return chatData;

}

I never write socket program . just know use mutil thread manage socket . but i think it's terrible. so i use netty . but i don't know how to use it for chatroom.

anybody can give me recommend ?

fred
  • 301
  • 1
  • 3
  • 6

3 Answers3

1

Make sure to position the reader index of the received ChannelBuffer to the position where the message you just read ends. @see ChannelBuffer.readerIndex(int) This is because ChannelBuffer.getByte(int) / .getBytes() do not alter the reader index.

If you don't, Netty will assume that you did not read any bytes from the buffer (handing it to the next handler in the pipeline if there is one) and throw the excetion you just encountered.

dcode
  • 11
  • 1
1

Just replace buffer.getByte(..) with buffer.readByte() to fix this.

Norman Maurer
  • 23,104
  • 2
  • 33
  • 31
0

I would suggest that you use the built in functionality for dealing with strings provided within the Netty API.

  • DelimiterBasedFrameDecoder
  • StringDecoder
  • StringEncoder

I tried to create links to the appropriate documentation pages, but stackoverflow prevents me from posting more than two links.

The Netty SecureChat Example provides a pretty clear explanation of how to uses these handlers on your channel pipeline.

Tim M.
  • 53,671
  • 14
  • 120
  • 163
Greenyyy
  • 48
  • 7