Last active
December 30, 2015 20:49
-
-
Save olostan/7883315 to your computer and use it in GitHub Desktop.
Dart: two-way isolate communication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "dart:isolate"; | |
void main() { | |
print("Starting"); | |
var sPort = new ReceivePort(); | |
SendPort rPort; | |
sPort.listen((msg) { | |
if (msg is SendPort) { | |
print("Host got port. sending back"); | |
rPort = msg; | |
rPort.send("Hello!"); | |
} | |
else print("Host got $msg"); | |
rPort.send(null); | |
sPort.close(); | |
}); | |
Isolate.spawn(test,sPort.sendPort); | |
} | |
void test(sender) { | |
var rPort = new ReceivePort(); | |
sender.send(rPort.sendPort); | |
rPort.listen((msg){ | |
print("Worker got $msg"); | |
if (msg!=null) | |
sender.send("I am worker"); | |
else rPort.close(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment