Skip to content

Instantly share code, notes, and snippets.

@nrw
Created January 30, 2014 22:40
Show Gist options
  • Save nrw/8721623 to your computer and use it in GitHub Desktop.
Save nrw/8721623 to your computer and use it in GitHub Desktop.
Example of failing reconnect
// client
var domready, reconnect, through;
through = require('through');
domready = require('domready');
reconnect = require('reconnect/shoe');
domready(function() {
var r, result;
result = document.getElementById('result');
// establish reconnections
r = reconnect(function(remote) {
var instream, outstream;
// the stream we'll type into
instream = through();
// the stream that comes back from the server
outstream = through(function(msg) {
console.log("client received message:", msg);
result.textContent += "client received message: " + msg;
return this.queue(msg);
});
instream.pipe(remote).pipe(outstream);
// make sure the write stream is accessible from the console
window.stream = instream;
}).connect('/sock');
document.body.appendChild(r.widget());
});
// install: npm install domready reconnect shoe through
// server: node server.js
// build client: browserify -o bundle.js client.js
// node server.js
var fs, http, server, shoe, sock, through;
http = require('http');
fs = require('fs');
shoe = require('shoe');
through = require('through');
// http server
server = http.createServer(function(req, res) {
console.log('path', req.url);
if (req.url === '/bundle.js') {
return res.end(fs.readFileSync("" + __dirname + "/bundle.js"));
} else if (req.url === '/') {
return res.end("<!DOCTYPE html><html><head><title></title>"+
"<script type='text/javascript' src='/bundle.js'></script>"+
"</head><body><div id='result'></div></body></html>");
} else {
return res.end('404');
}
});
// streaming sockjs
sock = shoe(function(remote) {
var caps;
caps = through(function(msg) {
console.log('server received message:', msg);
return this.queue(msg.toString().toUpperCase());
});
return remote.pipe(caps).pipe(remote);
});
sock.install(server, '/sock');
server.listen(3333);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment