Created
October 6, 2014 21:56
-
-
Save kgersen/148460d056f498a849ef to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>web worker sample</title> | |
</head> | |
<body> | |
<h1>web socket & web worker sample</h1> | |
<h1>check console / editor output windows</h1> | |
<script type="application/dart" src="main.dart"></script> | |
<script src="packages/browser/dart.js"></script> | |
</body> | |
</html> |
This file contains hidden or 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:html'; | |
import 'dart:isolate'; | |
import 'dart:async'; | |
void main() { | |
SendPort sendPort; | |
ReceivePort receive = new ReceivePort(); | |
receive.listen((msg) { | |
if (sendPort == null) { | |
sendPort = msg; | |
} else { | |
print('From isolate: $msg'); | |
} | |
}); | |
int counter = 0; | |
// create the worker, passes the ws url, then send a count every sec | |
Isolate.spawnUri(Uri.parse('ws_worker.dart'), ['ws://echo.websocket.org'], receive.sendPort).then((isolate) { | |
new Timer.periodic(const Duration(seconds:1), (t) { | |
sendPort.send('Count is ${counter++}'); | |
}); | |
}); | |
} |
This file contains hidden or 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:html'; | |
import 'dart:isolate'; | |
import 'dart:async'; | |
WebSocket ws = null; | |
void initWebSocket(SendPort sp, String url, [int retrySeconds = 2]) { | |
var reconnectScheduled = false; | |
void scheduleReconnect() { | |
if (!reconnectScheduled) { | |
new Timer(new Duration(milliseconds: 1000 * retrySeconds), () => initWebSocket(sp, url, retrySeconds * 2)); | |
} | |
reconnectScheduled = true; | |
} | |
sp.send("Connecting to websocket $url ..."); | |
ws = new WebSocket(url); | |
sp.send("websocket created"); | |
ws.onOpen.listen((e) { | |
sp.send('Connected'); | |
}); | |
ws.onClose.listen((e) { | |
sp.send('Websocket closed, retrying in $retrySeconds seconds'); | |
scheduleReconnect(); | |
}); | |
ws.onError.listen((e) { | |
sp.send("Error connecting to ws"); | |
scheduleReconnect(); | |
}); | |
ws.onMessage.listen((MessageEvent e) { | |
sp.send('Received message: ${e.data}'); | |
}); | |
} | |
// entry point of worker, expect a parameter: url of the websocker server | |
main(List<String> args, SendPort sendPort) { | |
ReceivePort receivePort = new ReceivePort(); | |
sendPort.send(receivePort.sendPort); | |
receivePort.listen((msg) { | |
if (ws == null) | |
initWebSocket(sendPort,args[0]); | |
else | |
ws.send('Hello from Dart!'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dart:
WebSocket within a WebWorker test.
adapted from : https://github.com/dart-lang/dart-samples/tree/master/html5/web/websockets/basics