poll method

Future<void> poll(
  1. String? url,
  2. SignalRHttpRequest pollOptions
)

Implementation

Future<void> poll(String? url, SignalRHttpRequest pollOptions) async {
  try {
    while (_running) {
      // We have to get the access token on each poll, in case it changes
      final token = await _getAccessToken();
      _updateHeaderToken(pollOptions, token);

      try {
        final pollUrl = "$url&_=${DateTime.now()}";
        _logger?.finest("(LongPolling transport) polling: $pollUrl");
        final response = await _httpClient.get(pollUrl, options: pollOptions);

        if (response.statusCode == 204) {
          _logger?.info("(LongPolling transport) Poll terminated by server");

          _running = false;
        } else if (response.statusCode != 200) {
          _logger?.severe(
              "(LongPolling transport) Unexpected response code: ${response.statusCode}");

          // Unexpected status code
          _closeError =
              HttpError(response.statusText ?? "", response.statusCode);
          _running = false;
        } else {
          // Process the response
          if (!isStringEmpty(response.content as String?)) {
            // _logger.log(LogLevel.Trace, "(LongPolling transport) data received. ${getDataDetail(response.content, this.logMessageContent)}");
            _logger?.finest("(LongPolling transport) data received");
            if (onReceive != null) {
              onReceive!(response.content);
            }
          } else {
            // This is another way timeout manifest.
            _logger?.finest(
                "(LongPolling transport) Poll timed out, reissuing.");
          }
        }
      } catch (e) {
        if (!_running) {
          // Log but disregard errors that occur after stopping
          _logger?.finest(
              "(LongPolling transport) Poll errored after shutdown: ${e.toString()}");
        } else {
          if (e is TimeoutError) {
            // Ignore timeouts and reissue the poll.
            _logger?.finest(
                "(LongPolling transport) Poll timed out, reissuing.");
          } else {
            // Close the connection with the error as the result.
            _closeError = Exception(e.toString());
            _running = false;
          }
        }
      }
    }
  } finally {
    _logger?.finest("(LongPolling transport) Polling complete.");

    // We will reach here with pollAborted==false when the server returned a response causing the transport to stop.
    // If pollAborted==true then client initiated the stop and the stop method will raise the close event after DELETE is sent.
    if (!this.pollAborted!) {
      _raiseOnClose();
    }
  }
}