Skip to content

Instantly share code, notes, and snippets.

@lxe
Created July 16, 2014 01:52
Show Gist options
  • Save lxe/048a193fb99edaa643a4 to your computer and use it in GitHub Desktop.
Save lxe/048a193fb99edaa643a4 to your computer and use it in GitHub Desktop.
// Long-polling chat server + client.
var chats = [];
require('http').createServer(function(req, res) {
if (/\/c/.test(req.url)) return res.end(chats.join('\n'));
var m = req.url.split('=');
if (m.length > 1) chats.unshift(m[1].replace(/\+/g, ' '));
res.end('<html><body><form><input name="m"></form><pre></pre><script>'
+ 'var r=new XMLHttpRequest;(function e(){r.open("GET","/c",true);r.send();r.onreadystatechange=function(){if(r.readyState==4){document.getElementsByTagName("pre")[0].innerHTML=r.responseText;setTimeout(e,1e3)}}})();document.forms[0].m.focus()'
+ '</script></body></html>')
}).listen(parseInt(process.env.PORT, 10)|| 3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment