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

时间:2014-06-05 17:28:53

标签: dart

我在服务器端摆弄飞镖。我试图通过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);
    });
}
}

这是我的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。

1 个答案:

答案 0 :(得分:2)

只需更换:

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