Permalink
Browse files

roll your own socket.io example files

  • Loading branch information...
1 parent 3a084e8 commit 3ea0fb9924872f16a71d5c380e37c8966634256f @substack committed Sep 7, 2012
@@ -0,0 +1,5 @@
+<html>
+<body>
+<script src="bundle.js"></script>
+</body>
+</html>
@@ -0,0 +1,19 @@
+var shoe = require('shoe');
+var emitStream = require('emit-stream');
+var JSONStream = require('JSONStream');
+
+var parser = JSONStream.parse([true]);
+var stream = parser.pipe(shoe('/sock')).pipe(parser);
+var ev = emitStream(stream);
+
+ev.on('lower', function (msg) {
+ var div = document.createElement('div');
+ div.textContent = msg.toLowerCase();
+ document.body.appendChild(div);
+});
+
+ev.on('upper', function (msg) {
+ var div = document.createElement('div');
+ div.textContent = msg.toUpperCase();
+ document.body.appendChild(div);
+});
@@ -0,0 +1,33 @@
+var http = require('http');
+var EventEmitter = require('events').EventEmitter;
+
+var server = http.createServer(require('ecstatic')(__dirname));
+server.listen(8080);
+
+var shoe = require('shoe');
+var emitStream = require('emit-stream');
+var JSONStream = require('JSONStream');
+
+var sock = shoe(function (stream) {
+ var ev = new EventEmitter;
+ emitStream(ev)
+ .pipe(JSONStream.stringify())
+ .pipe(stream)
+ ;
+
+ var intervals = [];
+
+ intervals.push(setInterval(function () {
+ ev.emit('upper', 'abc');
+ }, 500));
+
+ intervals.push(setInterval(function () {
+ ev.emit('lower', 'def');
+ }, 300));
+
+ stream.on('end', function () {
+ intervals.forEach(clearInterval);
+ });
+
+});
+sock.install(server, '/sock');

0 comments on commit 3ea0fb9

Please sign in to comment.