start method

  1. @override
void start()
override

Start listening for incoming clients.

Implementation

@override
void start() {
  receivePort.listen((data) {
    if (data is SendPort) {
      var id = _uuid.v4();
      _clients[id] = data;
      data.send(MessageHandler().encodeSendPortResponseMessage(id));
    } else if (data is Map<String, Object?>) {
      var (id, method, requestId, params) =
          MessageHandler().decodeRequestMessage(data);
      var (clientId, eventName, subscriptionId, value) =
          MessageHandler().decodeRequestParams(params);

      var sp = _clients[id];
      if (sp == null) {
        // There's nobody to respond to, so don't send anything to anyone
        return;
      }

      if (method == 'publish') {
        if (eventName == null || value == null) {
          sp.send(MessageHandler().encodePublishResponseError(requestId));
        }
        var rq = _IsolatePublishRequestImpl(
            requestId, clientId, eventName, value, sp);
        _onPublish.add(rq);
      } else if (method == 'subscribe') {
        if (eventName == null) {
          sp.send(
              MessageHandler().encodeSubscriptionResponseError(requestId));
        }
        var rq = _IsolateSubscriptionRequestImpl(
            clientId, eventName, sp, requestId, _uuid);
        _onSubscribe.add(rq);
      } else if (method == 'unsubscribe') {
        if (subscriptionId == null) {
          sp.send(
              MessageHandler().encodeUnsubscriptionResponseError(requestId));
        }
        var rq = _IsolateUnsubscriptionRequestImpl(
            clientId, subscriptionId, sp, requestId);
        _onUnsubscribe.add(rq);
      } else {
        sp.send(MessageHandler()
            .encodeUnknownMethodResponseError(requestId, method));
      }
    }
  });
}