当前位置: 动力学知识库 > 问答 > 编程问答 >

dart: howto output stream data to websocket

问题描述:

im fiddling around with dart on the server side. I'm trying to send data periodically to clients connected to my server via websockets.

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);

});

}

}

This is my WebSocketHandler, which works fine for messages but the stream.listen in combination with the socket.add producess the following error:

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)

Here is the Stacktrace.

网友答案:

Just replace this:

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

with this:

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

Full source:

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>

Client:

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>');
  });
}

Server:

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());
        });
      });
    });
  });
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: