fetchData method

Future<void> fetchData()

Implementation

Future<void> fetchData() async {
  if (!_isConnected) {
    onResponseUpdate('连接未建立');
    return;
  }

  try {
    final response = await http.get(Uri.parse('https://jsonplaceholder.typicode.com/todos/1'))
      .timeout(Duration(seconds: 10));

    final data = jsonDecode(response.body);
    _dataChannel!.send(RTCDataChannelMessage(jsonEncode(data)));
  } on TimeoutException {
    onResponseUpdate('请求超时');
  } catch (e) {
    onResponseUpdate('数据传输失败: ${e.toString()}');
  }
}