gpt4 book ai didi

dart - dart:如何将流数据输出到websocket

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

我在服务器端摆弄 Dart 。我正在尝试定期将数据发送到通过websocket连接到我的服务器的客户端。

import 'dart:io';
import 'dart:async';

class WebSocketHandler {
static void handleWebSocket(WebSocket socket) {
print("Client connected!");
var stream = new Stream.periodic(const Duration(seconds: 5), (count) {
return count;
});

stream.listen((num) {
print(num);
socket.add(num);
});

socket.listen((message) {
print("Client send: $message");
socket.add(message);
});
}
}

这是我的WebSocketHandler,对消息运行良好,但是stream.listen与socket.add结合使用会产生以下错误:
Uncaught Error: Illegal argument(s): 0
Stack Trace:
#0 _WebSocketOutgoingTransformer.add (dart:io/websocket_impl.dart:502)
#1 _handleData (dart:async/stream_transformers.dart:116)
#2 _rootRunUnary (dart:async/zone.dart:730)
#3 _RootZone.runUnary (dart:async/zone.dart:864)
#4 _BaseZone.runUnaryGuarded (dart:async/zone.dart:582)
#5 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:333)
#6 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263)
#7 _StreamController&&_SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:535)
#8 _StreamController._add (dart:async/stream_controller.dart:442)
#9 _StreamController.add (dart:async/stream_controller.dart:399)
#10 _WebSocketConsumer.addStream.<anonymous closure> (dart:io/websocket_impl.dart:709)
#11 _rootRunUnary (dart:async/zone.dart:730)
#12 _RootZone.runUnary (dart:async/zone.dart:864)
#13 _BaseZone.runUnaryGuarded (dart:async/zone.dart:582)
#14 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:333)
#15 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263)
#16 _StreamController&&_SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:535)
#17 _StreamController._add (dart:async/stream_controller.dart:442)
#18 _StreamController.add (dart:async/stream_controller.dart:399)
#19 _StreamSinkImpl.add (dart:io/io_sink.dart:143)
#20 _WebSocketImpl.add (dart:io/websocket_impl.dart:928)
#21 WebSocketHandler.handleWebSocket.<anonymous closure> (file:///D:/Webdev/Websites/webradio-dart/server/WebSocketHandler.dart:13:14)
#22 _rootRunUnary (dart:async/zone.dart:730)
#23 _RootZone.runUnary (dart:async/zone.dart:864)
#24 _BaseZone.runUnaryGuarded (dart:async/zone.dart:582)
#25 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:333)
#26 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263)
#27 _StreamController&&_SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:535)
#28 _StreamController._add (dart:async/stream_controller.dart:442)
#29 _StreamController.add (dart:async/stream_controller.dart:399)
#30 Stream.Stream.periodic.sendEvent (dart:async/stream.dart:118)
#31 Stream.Stream.periodic.startPeriodicTimer.<anonymous closure> (dart:async/stream.dart:124)
#32 _handleTimeout (dart:io/timer_impl.dart:292)
#33 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:124)


Unhandled exception:
Illegal argument(s): 0
#0 _rootHandleUncaughtError.<anonymous closure>.<anonymous closure> (dart:async/zone.dart:713)
#1 _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:23)
#2 _asyncRunCallback (dart:async/schedule_microtask.dart:32)
#3 _asyncRunCallback (dart:async/schedule_microtask.dart:36)
#4 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:128)

这是Stacktrace。

最佳答案

只需替换为:

stream.listen((num) {
print(num);
socket.add(num);
});

有了这个:

stream.listen((num) {
print(num);
socket.add(num.toString());
});

完整资料:

的HTML:

<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<title>ClientTest</title>
<link rel="stylesheet" href="clientstreamperiodictest.css">
</head>
<body>
<p>Response:</p>

<div id="response">
</div>

<script type="application/dart" src="clientstreamperiodictest.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>

客户:

import 'dart:html';
void main() {
WebSocket ws = new WebSocket('ws://127.0.0.1:4040');
ws.onMessage.listen((MessageEvent e) {
querySelector('#response').appendHtml('<p>${e.data}</p>');
});
}

服务器:

import 'dart:io';
import 'dart:async';
main() {
HttpServer.bind('127.0.0.1', 4040).then((server) {
server.listen((HttpRequest request) {
WebSocketTransformer.upgrade(request).then((socket) {
var streamPeriodic =
new Stream.periodic(const Duration(seconds: 5), (count) {
return count;
});
streamPeriodic.listen((num) {
print(num);
socket.add(num.toString());
});
});
});
});
}

关于dart - dart:如何将流数据输出到websocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24066510/

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