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

Commit edb17d2

Browse files
IgorMinarbtford
authored andcommitted
feat: add websockets example
1 parent d725f46 commit edb17d2

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

example/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ <h1>Examples</h1>
1616
<li><a href="counting.html">Counting Tasks</a></li>
1717
<li><a href="profiling.html">Profiling Across Tasks</a></li>
1818
<li><a href="throttle.html">Throttle</a></li>
19+
<li><a href="web-socket.html">WebSocket</a></li>
1920
<li><a href="xml-http-request.html">XMLHttpRequest</a></li>
2021
</ol>
2122

example/web-socket.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>WebSockets with Zones</title>
6+
<link rel="stylesheet" href="style.css">
7+
<script src="../zone.js"></script>
8+
</head>
9+
<body>
10+
11+
<p>
12+
Ensure that you started <code>node test/ws-server.js</code> before loading
13+
this page. Then check console output.
14+
</p>
15+
16+
<script>
17+
18+
var ws = new WebSocket('ws://localhost:8001');
19+
20+
ws.onopen = function() {
21+
console.log('Setting secret payload in the current zone (id: %d)', zone.$id);
22+
zone.secretPayload = 'bah!';
23+
24+
ws.onmessage = function(event) {
25+
if (!zone.hasOwnProperty('secretPayload') &&
26+
zone.parent.hasOwnProperty('secretPayload') &&
27+
zone.secretPayload === 'bah!') {
28+
console.log("The current zone (id: %d) doesn't have secretPayload, but parent (id: %d) does. Zones are working!",
29+
zone.$id, zone.parent.$id);
30+
} else {
31+
console.error('Secret payload not found where expected! Zones are not working! :-(');
32+
}
33+
};
34+
ws.send('hello!');
35+
};
36+
37+
</script>
38+
</body>
39+
</html>

0 commit comments

Comments
 (0)