Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
first commit
- Loading branch information
Showing
8 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
var express = require('express'); | ||
|
||
var app = express.createServer(); | ||
|
||
// Configuration | ||
|
||
app.set('views', __dirname + '/views'); | ||
app.set('view engine', 'ejs'); | ||
|
||
|
||
|
||
// Routes | ||
|
||
app.get('/', function(req, res){ | ||
res.render('index', {locals: { | ||
title: 'NowJS + Express Example' | ||
}}); | ||
}); | ||
|
||
app.get('/chat', function(req, res){ | ||
res.render('chat', {locals: { | ||
title: 'NowJS + Express Example' | ||
}}); | ||
}); | ||
|
||
app.listen(8080); | ||
console.log("Express server listening on port %d", app.address().port); | ||
|
||
|
||
// NowJS component | ||
|
||
var everyone = require("now").initialize(app); | ||
|
||
|
||
everyone.connected(function(){ | ||
console.log("Joined: " + this.now.name); | ||
}); | ||
|
||
|
||
everyone.disconnected(function(){ | ||
console.log("Left: " + this.now.name); | ||
}); | ||
|
||
everyone.now.distributeMessage = function(message){ | ||
everyone.now.receiveMessage(this.now.name, message); | ||
}; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<h2>Chat Window</h2> | ||
<div id="messages"></div> | ||
<div style="position: absolute; bottom: 25;"> | ||
<input type="text" id="text-input"> | ||
<input type="button" value="Send" id="send-button"> | ||
</div> | ||
|
||
<script> | ||
$(document).ready(function(){ | ||
now.receiveMessage = function(name, message){ | ||
$("#messages").append("<br>" + name + ": " + message); | ||
} | ||
$("#send-button").click(function(){ | ||
now.distributeMessage($("#text-input").val()); | ||
$("#text-input").val(""); | ||
$("#text-input").focus(); | ||
}); | ||
now.name = prompt("What's your name?", ""); | ||
}); | ||
</script> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<h1><%= title %></h1> | ||
<p>Welcome to <%= title %></p> | ||
<p>This is an example of an embedded chat window added to any site. <a href='#' id='chat'>Chat with us</a></p> | ||
<div id="floater" style="position: absolute; right: 0; top:0; width="25%" height="100%"></div> | ||
<script> | ||
$(document).ready(function() { | ||
$('#chat').click(function(e) { | ||
e.preventDefault(); | ||
$('<iframe width="300px" height="100%" sandbox="allow-same-origin allow-forms allow-scripts" src="http://localhost:8080/chat"></iframe>').appendTo('#floater'); | ||
}); | ||
}); | ||
</script> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title><%= title %></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> | ||
</head> | ||
<body> | ||
<%- body %> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<!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(name, message){ | ||
$("#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> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
var fs = require('fs'); | ||
var server = require('http').createServer(function(req, response){ | ||
fs.readFile(__dirname+'/helloworld.html', function(err, data){ | ||
response.writeHead(200, {'Content-Type':'text/html'}); | ||
response.write(data); | ||
response.end(); | ||
}); | ||
}); | ||
server.listen(8080); | ||
var nowjs = require("now"); | ||
var everyone = nowjs.initialize(server); | ||
|
||
everyone.on("connect", function(){ | ||
console.log("Joined: " + this.now.name); | ||
}); | ||
|
||
everyone.on("disconnect", function(){ | ||
console.log("Left: " + this.now.name); | ||
}); | ||
|
||
everyone.now.distributeMessage = function(message){ | ||
everyone.now.receiveMessage(this.now.name, message); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<!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(name, message){ | ||
$("#messages").append("<br>" + name + ": " + message); | ||
} | ||
|
||
$("#send-button").click(function(){ | ||
now.distributeMessage($("#text-input").val()); | ||
$("#text-input").val(""); | ||
}); | ||
|
||
$(".change").click(function(){ | ||
now.changeRoom($(this).text()); | ||
}); | ||
|
||
now.name = prompt("What's your name?", ""); | ||
|
||
}); | ||
</script> | ||
</head> | ||
|
||
<body> | ||
|
||
<div> | ||
<a href="#" class="change selected">room 1</a> - | ||
<a href="#" class="change">room 2</a> - | ||
<a href="#" class="change">room 3</a> | ||
</div> | ||
<br> | ||
<div id="messages"><br>You're in room 1</div> | ||
<input type="text" id="text-input"> | ||
<input type="button" value="Send" id="send-button"> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
var fs = require('fs'); | ||
var server = require('http').createServer(function(req, response){ | ||
fs.readFile(__dirname+'/multiroomchat.html', function(err, data){ | ||
response.writeHead(200, {'Content-Type':'text/html'}); | ||
response.write(data); | ||
response.end(); | ||
}); | ||
}); | ||
server.listen(8080); | ||
|
||
|
||
var nowjs = require("now"); | ||
var everyone = nowjs.initialize(server); | ||
|
||
|
||
everyone.on('connect', function(){ | ||
this.now.room = "room 1"; | ||
nowjs.getGroup(this.now.room).addUser(this.user.clientId); | ||
console.log("Joined: " + this.now.name); | ||
}); | ||
|
||
|
||
everyone.on('disconnect', function(){ | ||
console.log("Left: " + this.now.name); | ||
}); | ||
|
||
everyone.now.changeRoom = function(newRoom){ | ||
nowjs.getGroup(this.now.room).removeUser(this.user.clientId); | ||
nowjs.getGroup(newRoom).addUser(this.user.clientId); | ||
this.now.room = newRoom; | ||
this.now.receiveMessage("SERVER", "You're now in " + this.now.room); | ||
} | ||
|
||
everyone.now.distributeMessage = function(message){ | ||
nowjs.getGroup(this.now.room).now.receiveMessage(this.now.name, message); | ||
}; |