gpt4 book ai didi

dart - 如何在Rikulo的 Dart 流服务器上调用Web服务?

转载 作者:行者123 更新时间:2023-12-03 03:29:29 25 4
gpt4 key购买 nike

尝试在Stream服务器上使用静态Web服务时遇到技术问题。

我使用HTTPClient.openUrl从另一个远程服务器检索JSON响应,但是一旦打开连接,就无法再将响应(connect.response.write)写入我的浏览器客户端。

该错误列出如下:

    Unhandled exception:
Bad state: StreamSink is closed
#0 _FutureImpl._scheduleUnhandledError.<anonymous closure> (dart:async:325:9)
#1 Timer.run.<anonymous closure> (dart:async:2251:21)
#2 Timer.run.<anonymous closure> (dart:async:2259:13)
#3 Timer.Timer.<anonymous closure> (dart:async-patch:15:15)
#4 _Timer._createTimerHandler._handleTimeout (dart:io:6730:28)
#5 _Timer._createTimerHandler._handleTimeout (dart:io:6738:7)
#6 _Timer._createTimerHandler.<anonymous closure> (dart:io:6746:23)
#7 _ReceivePortImpl._handleMessage (dart:isolate-patch:81:92)

有人知道在流服务器上调用Web服务的正确方法吗?

最佳答案

关键是您必须返回Future,因为您的任务(openUrl)是异步的。在示例代码中,您必须执行以下操作:

return conn.then((HttpClientRequest request) {
//^-- notice: you must return a future to indicate when the serving is done

有关更多信息,请引用 Request Handling。为了避免这种错误,我发布了 a feature request here

这是一个工作示例:
library issues;

import "dart:io";
import "dart:uri";
import "package:rikulo_commons/io.dart" show IOUtil;
import "package:stream/stream.dart";

void main() {
new StreamServer(uriMapping: {
"/": (connect)
=> new HttpClient().getUrl(new Uri("http://google.com"))
.then((req) => req.close())
.then((res) => IOUtil.readAsString(res))
.then((result) {
connect.response.write(result);
})
}).start();
}

关于dart - 如何在Rikulo的 Dart 流服务器上调用Web服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16311733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com