voiceWebsocket method

Future voiceWebsocket({
  1. required String baseWsUrl,
  2. required String mentor,
  3. required String tenantKey,
  4. required String username,
  5. required String sessionId,
  6. required String token,
  7. required List<int> voiceData,
})

Implementation

Future<dynamic> voiceWebsocket({
  required String baseWsUrl,
  required String mentor,
  required String tenantKey,
  required String username,
  required String sessionId,
  required String token,
  required List<int> voiceData, // voice data as bytes
}) async {
  // Establishing a WebSocket connection
  final channel = WebSocketChannel.connect(
    Uri.parse('wss://$baseWsUrl/ws/langflow-voice/'),
  );

  // Prepare auth payload
  final authData = json.encode({
    "flow": {
      "name": mentor,
      "tenant": tenantKey,
      "username": username,
    },
    "session_id": sessionId,
    "token": token,
  });

  // Send auth payload
  channel.sink.add(authData);

  // Convert voice data to Blob if necessary or directly send bytes
  channel.sink.add(voiceData);

  // Listen for server responses
  channel.stream.listen((message) {
    // Process incoming messages
    print('Server: $message');
  }, onDone: () {
    // When the communication is done
    print('Connection closed by server.');
  }, onError: (error) {
    // Handle errors
    print('Error: $error');
  });

  // Schedule the WebSocket connection to close after 12 seconds
  Timer(Duration(seconds: 12), () {
    print('Closing connection after 12 seconds.');
    channel.sink.close();
  });
}