Created
November 24, 2010 06:22
-
-
Save matthewfl/713200 to your computer and use it in GitHub Desktop.
A really basic chat server on http://chat.jsapp.us
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Chat</title> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> | |
<script> | |
$(function () { | |
function check () { | |
$("#output").load('/update', function () { | |
setTimeout(check, 1); | |
}); | |
} | |
function send () { | |
$.post("/send", $("#text").val()); | |
$("#text").val(''); | |
} | |
check(); | |
$("#send").click(send); | |
$("#text").keypress(function (e) { | |
if(e.keyCode == 13) send(); | |
}) | |
}); | |
</script> | |
</head> | |
<body> | |
<h3>This is a really simple demo that I wrote to quickly test out jsapp.us, the code is only about 15 lines longs and uses node-router</h3> | |
<p>I came back and added long polling, it took about 15 min</p> | |
<div id="output">{text}</div> | |
<br> | |
<input id="text"></input><button id="send">Send</button> | |
<br> | |
<a href="https://gist.github.com/713200">Code</a> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// chat application | |
var db = require('db'); | |
var router = require('matthewfl/node-router'), // this could be ./node-router if you have node-router save in you jsapp filesystem | |
server = router.getServer(), | |
fs = require('fs'); | |
var updates = []; | |
var clients = []; | |
var fileContent = null; | |
server.get('/', function (req, res) { | |
if(fileContent) { | |
return fileContent.replace("{text}", "<p>"+updates.join("</p><p>")+"</p>") | |
}else{ | |
fs.readFile('chat.index.html', function (err, content) { | |
res.writeHead(200, {"Content-type": "text/html"}); | |
res.end(content.replace("{text}", "")); | |
fileContent = content; | |
}); | |
} | |
}); | |
server.get('/update', function (req, res) { | |
res.writeHead(200, {"Content-type":"text/html"}); | |
clients.push(res); | |
}); | |
server.post('/send', function (req, res) { | |
var data=""; | |
req.on('data', function (d) { | |
data += d.toString('ascii'); | |
}); | |
req.on('end', function () { | |
updates.push(data); | |
res.end(); | |
var t = "<p>"+updates.join("</p><p>")+"</p>"; | |
while(clients.length) { | |
clients.pop().end(t); | |
} | |
}) | |
}); | |
server.listen(8000); |
I know this is really old - but the chat example page doesn't work.. .. and to top it off, a few minutes after trying it, the page is now giving a server error.
doesn't work for me too!
every comment goes to the bit bucket, and doesn't show on my screen
does there need to be two (or more) connections to the server for it to work?
now i see 'server error' message, no chat app
now i see a chat app, that sends every comment to the bit bucket (again)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry about that, './node-router' is attempting to load node-router out of your local jsapp file system. There are two ways to fix this, the first is to save node-router into a file called node-router on your account. The second is to replace it with 'matthewfl/node-router'. The reason this second way works is that I have decided to make my node-router file public, and thus anyone can import that file.
To control what files are public use the files command in the editor.