Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit a605b31e2336757daa67b3da489e15ec3c573442 1 parent 4b1c3de
Eric Zhang ericz authored
47 examples/express_example/app.js
View
@@ -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);
+};
+
24 examples/express_example/views/chat.ejs
View
@@ -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>
13 examples/express_example/views/index.ejs
View
@@ -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>
11 examples/express_example/views/layout.ejs
View
@@ -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>
30 examples/helloworld_example/helloworld.html
View
@@ -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>
23 examples/helloworld_example/helloworld_server.js
View
@@ -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);
+};
41 examples/multiroomchat_example/multiroomchat.html
View
@@ -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>
36 examples/multiroomchat_example/multiroomchat_server.js
View
@@ -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);
+};
Please sign in to comment.
Something went wrong with that request. Please try again.