Dart Isolates As Workers

后端 未结 4 639
不思量自难忘°
不思量自难忘° 2021-02-09 20:35

Edited to make the question more clear.

I am trying to work with Isolates (or Web Workers) in Dart. The only ways I can find to communicate between the

4条回答
  •  执念已碎
    2021-02-09 20:48

    You can now use the MessageBox class to communicate the other way around. This code sends a message from the Isolate code as soon as it receives the Sink end of the MessageBox. Main thread receives the messages sent from the Isolate and prints it on the console of Dartium. Once you receive the Sink you can launch your game logic and send updates using the sink object received.

    import 'dart:html';
    import 'dart:isolate';
    
    void main() {
      IsolateSink isolateSink = streamSpawnFunction(myIsolateEntryPoint);
      MessageBox isolateMessageBox = new MessageBox();
      isolateSink.add(isolateMessageBox.sink);
      isolateMessageBox.stream.listen((String data) {
        print(data);
      });
    }
    
    void myIsolateEntryPoint() {
      stream.listen((IsolateSink messageBoxSink) {
        messageBoxSink.add("Test");
      });
    }
    

提交回复
热议问题