add method

The message frame completes, or null while one is still being fragmented.

A control frame comes back as is.

Implementation

WebSocketInbound? add(WebSocketFrame frame) {
  if (frame.opcode.isControl) return WebSocketControl(frame);
  final opcode = _opcode ??= frame.opcode;
  final limit = maxMessageSize;
  if (limit != null && _parts.length + frame.payload.length > limit) {
    throw WebSocketProtocolException(1009, 'Message too big');
  }
  _parts.add(frame.payload);
  if (!frame.fin) return null;
  _opcode = null;
  final payload = _parts.takeBytes();
  if (opcode != WebSocketOpcode.text) return WebSocketBinary(payload);
  try {
    return WebSocketText(utf8.decode(payload));
  } on FormatException {
    throw WebSocketProtocolException(1007, 'Text is not valid UTF-8');
  }
}