原因是浏览器对大数据量请求时,会自动将数据进行分片传输。数据会以ContinuationFrame的形式发送, 直到isFinalFragment为true时结束, 中间不会穿插其它的Frame。
可针对ContinuationFrame的数据进行拼接,得到完整数据后再进行业务操作。
import io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame;
private StringBuilder frameBuffer = null;
protected void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame)
{
if (frame instanceof TextWebSocketFrame) {
frameBuffer = new StringBuilder();
frameBuffer.append(((TextWebSocketFrame)frame).text());
}
else if (frame instanceof ContinuationWebSocketFrame) {
if (frameBuffer != null) {
frameBuffer.append(((ContinuationWebSocketFrame)frame).text());
}
else {
logger.warn("Continuation frame received without initial frame.");
}
}
else {
throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass().getName()));
}
// Check if Text or Continuation Frame is final fragment and handle if needed.
if (frame.isFinalFragment()) {
handleMessageCompleted(ctx, frameBuffer.toString());
frameBuffer = null;
}
}
参考: