Пример #1
0
 @Override
 public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
   Channel channel = ctx.getChannel();
   if (!sessions.removeSession(channel.getId())) {
     Log.error("删除Session失败! sockId: " + channel.getId());
   }
 }
Пример #2
0
  private String getUtf(ChannelBuffer buffer) {
    // UTF = short + data;
    int readableBytes = buffer.readableBytes();
    if (readableBytes < 2) {
      throw new IllegalArgumentException("buffer doesn't contains utf data!");
    }

    // 只有长度 没有内容
    if (readableBytes == 2) {
      return "";
    }

    int dataLen = buffer.readUnsignedShort();
    byte[] data = new byte[dataLen];
    buffer.readBytes(data);

    try {
      return new String(data, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      Log.error("解码UTF错误", e);
    }

    return null;
  }