didConnectRequestLoop function

Future<void> didConnectRequestLoop(
  1. InfraDIDCommAgent agent,
  2. Context context,
  3. int loopTimeSeconds,
  4. dynamic loopCallback(
    1. String encodedMessage
    ),
)

Executes a loop that sends DIDConnectRequestMessage to the agent until a DIDAuthInit message is received.

The loop sends the message with a specified interval of loopTimeSeconds seconds. The loopCallback function is called with the encoded message as a parameter.

The loop starts by disconnecting the websocket client and then repeatedly connects and disconnects the client until a DIDAuthInit message is received. The loop waits for a specified interval of loopTimeSeconds seconds between each message.

Parameters:

  • agent: The InfraDIDCommAgent instance.
  • context: The context for the DIDConnectRequestMessage.
  • loopTimeSeconds: The interval between each message in seconds.
  • loopCallback: The function to call with the encoded message.

Returns: A Future that completes when the loop is stopped.

Implementation

Future<void> didConnectRequestLoop(
  InfraDIDCommAgent agent,
  Context context,
  int loopTimeSeconds,
  Function(String encodedMessage) loopCallback,
) async {
  // Disconnect the websocket client
  await agent.disconnect();

  while (!agent.isReceivedDIDAuthInit) {
    // Connect or reconnect the client
    await Future.delayed(Duration(milliseconds: 100));
    await agent.disconnect();
    await Future.delayed(Duration(milliseconds: 100));
    await agent.connect();

    int currentTime = DateTime.now().millisecondsSinceEpoch ~/ 1000;
    await Future.delayed(Duration(milliseconds: 500));
    String socketId = (await agent.socketId)!;

    Initiator initiator = Initiator(
      type: agent.role,
      serviceEndpoint: agent.url,
      socketId: socketId,
    );

    DIDConnectRequestMessage didConnectRequestMessage =
        DIDConnectRequestMessage(
      from: agent.did,
      createdTime: currentTime,
      expiresTime: currentTime + loopTimeSeconds,
      context: context,
      initiator: initiator,
    );

    final encodedMessage =
        didConnectRequestMessage.encode(CompressionLevel.compactJSON);

    loopCallback(encodedMessage);

    // Wait for the specified loop time
    await Future.delayed(Duration(seconds: loopTimeSeconds));
  }
}