emitWithAck method

void emitWithAck(
  1. String event,
  2. dynamic data, {
  3. Function? ack,
  4. bool binary = false,
})

Implementation

void emitWithAck(String event, dynamic data,
    {Function? ack, bool binary = false}) {
  if (socketEvents.contains(event)) {
    super.emit(event, data);
  } else {
    var packet = {};
    var sendData = data == null ? [event] : [event, data];

    var flags = this.flags ?? {};

    if (ack != null) {
      if (roomList.isNotEmpty || flags['broadcast'] == true) {
        throw UnsupportedError(
            'Callbacks are not supported when broadcasting');
      }

      acks['${nsp.ids}'] = ack;
      packet['id'] = '${nsp.ids++}';
    }

    packet['type'] = binary ? binaryEventValue : eventValue;
    packet['data'] = sendData;

    if (roomList.isNotEmpty || flags['broadcast'] == true) {
      adapter.broadcast(packet, {
        'except': [id],
        'rooms': roomList,
        'flags': flags
      });
    } else {
      // dispatch packet
      this.packet(packet,
          {'volatile': flags['volatile'], compress: flags['compress']});
    }
    roomList = [];
    this.flags = null;
  }
}