Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
feat: add websockets example
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorMinar authored and btford committed May 1, 2015
1 parent d725f46 commit edb17d2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ <h1>Examples</h1>
<li><a href="counting.html">Counting Tasks</a></li>
<li><a href="profiling.html">Profiling Across Tasks</a></li>
<li><a href="throttle.html">Throttle</a></li>
<li><a href="web-socket.html">WebSocket</a></li>
<li><a href="xml-http-request.html">XMLHttpRequest</a></li>
</ol>

Expand Down
39 changes: 39 additions & 0 deletions example/web-socket.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>WebSockets with Zones</title>
<link rel="stylesheet" href="style.css">
<script src="../zone.js"></script>
</head>
<body>

<p>
Ensure that you started <code>node test/ws-server.js</code> before loading
this page. Then check console output.
</p>

<script>

var ws = new WebSocket('ws://localhost:8001');

ws.onopen = function() {
console.log('Setting secret payload in the current zone (id: %d)', zone.$id);
zone.secretPayload = 'bah!';

ws.onmessage = function(event) {
if (!zone.hasOwnProperty('secretPayload') &&
zone.parent.hasOwnProperty('secretPayload') &&
zone.secretPayload === 'bah!') {
console.log("The current zone (id: %d) doesn't have secretPayload, but parent (id: %d) does. Zones are working!",
zone.$id, zone.parent.$id);
} else {
console.error('Secret payload not found where expected! Zones are not working! :-(');
}
};
ws.send('hello!');
};

</script>
</body>
</html>

0 comments on commit edb17d2

Please sign in to comment.