Skip to content

Commit

Permalink
added nodeclient support and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadmv committed Dec 10, 2011
1 parent 7de3daf commit ee1e703
Show file tree
Hide file tree
Showing 4 changed files with 637 additions and 0 deletions.
16 changes: 16 additions & 0 deletions examples/nodeclient_example/chat.js
@@ -0,0 +1,16 @@
var nowjs = require('../../lib/nodeclient/now.js');
var now = nowjs.nowInitialize('http://localhost:8080');
var readline = require('readline');
var rl = readline.createInterface(process.stdin, process.stdout);
rl.on('line', function(line){
now.distributeMessage(line);
});
now.ready(function(){
console.log("Chat server running!");
rl.question("What's your name? ",function(answer){
now.name = answer;
});
});
now.receiveMessage = function(message,name){
console.log("----"+name+": "+message);
}
32 changes: 32 additions & 0 deletions examples/nodeclient_example/index.html
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>nowjs test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script src="/nowjs/now.js"></script>

<script>
$(document).ready(function(){
now.receiveMessage = function(message,name){
$("#messages").append("<br>" + name + ": " + message);
}

$("#send-button").click(function(){
now.distributeMessage($("#text-input").val());
$("#text-input").val("");
});


now.name = prompt("What's your name?", "");

});
</script>
</head>

<body>
<div id="messages"></div>
<input type="text" id="text-input">
<input type="button" value="Send" id="send-button">
</body>
</html>

25 changes: 25 additions & 0 deletions examples/nodeclient_example/server.js
@@ -0,0 +1,25 @@
var http = require('http');
var sys = require('util');
var nowjs = require('now');
var fs = require('fs');
var server = http.createServer(function (req,res){
fs.readFile('./index.html', function(error, content) {
if (error) {
res.writeHead(500);
res.end();
}
else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(content, 'utf-8');
}
});
});
everyone = nowjs.initialize(server,{socketio: {"log level": 3}});
everyone.now.log = function(str){
console.log(str);
}
everyone.now.distributeMessage = function(str){
everyone.now.receiveMessage(str,this.now.name);
}
server.listen(8080);
console.log("Listening on 8080");

0 comments on commit ee1e703

Please sign in to comment.