Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

made the nowjs part also work. still having problems with synchronizi…

…ng the paramters though
  • Loading branch information...
commit 029dde8d342be8c1077a894cc7a56e7af36503cf 1 parent 25ff7f6
@jettro authored
Showing with 50 additions and 35 deletions.
  1. +26 −11 nowjs.js
  2. +22 −20 public/javascript/local-now.js
  3. +2 −4 views/now/index.jade
View
37 nowjs.js
@@ -13,42 +13,57 @@ NowjsApp.prototype.index = function(req, res) {
};
// nowjs configuration
-var now = require("/Users/jcoenradie/sources/external/nodejs/now");
+var now = require('now');
var everyone;
+var buffer = [];
NowjsApp.prototype.init = function(app) {
addToLog("Initializing the nowjs app");
- everyone = now.initialize(app, {clientWrite: true});
+ everyone = now.initialize(app, {clientWrite: false});
everyone.now.availablePersons = [];
everyone.now.distributeMessage = function(message) {
- console.log('Received a message to distribute: %s for %s', message, this.user.clientId);
- everyone.now.receiveMessage(this.user.clientId, message);
+ console.log('Received a message to distribute: %s for %s', message, everyone.now.availablePersons[this.user.clientId]);
+ var msg = { chat: [everyone.now.availablePersons[this.user.clientId], message] };
+ buffer.push(msg);
+ if (buffer.length > 15) buffer.shift();
+
+ everyone.now.receiveMessage(msg);
};
- everyone.now.setName = function() {
- addToLog("Set name to: " + this.now.name);
- everyone.now.availablePersons[this.user.clientId] = this.now.name;
- everyone.now.refreshPersonsList();
+ everyone.now.setName = function(name) {
+ addToLog("Set name to: " + name);
+ everyone.now.availablePersons[this.user.clientId] = name;
+ everyone.now.newlyJoined(name);
+ sendClients();
+
};
everyone.on('connect', function() {
- everyone.now.availablePersons[this.user.clientId] = this.user.clientId;
- everyone.now.newlyJoined(this.user.clientId);
addToLog("Joined: " + this.user.clientId);
+ this.now.bufferedMessages({buffer:buffer});
});
everyone.on('disconnect', function() {
addToLog("Left: " + this.user.clientId);
delete everyone.now.availablePersons[this.user.clientId];
everyone.now.hasLeft(this.user.clientId);
- everyone.now.refreshPersonsList();
+ sendClients();
});
function addToLog(message) {
console.log(message);
}
+ function sendClients() {
+ var curClients = [];
+ for (var i in everyone.now.availablePersons) {
+ curClients[curClients.length] = everyone.now.availablePersons[i];
+ }
+ console.log("Number of clients: " + curClients);
+ everyone.now.refreshAvailablePersons({users: curClients});
+ }
+
};
module.exports = NowjsApp;
View
42 public/javascript/local-now.js
@@ -1,5 +1,9 @@
-function writeMessage(message) {
- $("#message").prepend("<div>" + message + "</div>");
+function writeMessage(obj) {
+ $("#message").prepend("<div>" + obj.message[0] + " : " + obj.message[1] + "</div>");
+}
+
+function writeChat(obj) {
+ $("#chat").prepend("<div>" + obj.chat[0] + " : " + obj.chat[1] + "</div>");
}
function sendMessage() {
@@ -8,36 +12,34 @@ function sendMessage() {
}
function setName() {
- now.ready(function() {
- now.name = $("#visitorName").val();
- });
- writeMessage("Name is set to: " + $("#visitorName").val());
- now.setName();
-// now.enterGeneralRoom();
+ writeMessage({message:['You',"Name is set to: " + $("#visitorName").val()]});
+ now.setName($("#visitorName").val());
}
$(document).ready(function() {
- now.receiveMessage = function(name, message) {
- writeMessage(name + ": " + message);
+ now.receiveMessage = function(message) {
+ writeChat(message);
};
now.newlyJoined = function(name) {
- writeMessage(name + " Joined");
+ writeMessage({message:['System', name + " Joined"]});
+ refreshPersonsList();
};
now.hasLeft = function(name) {
- writeMessage(name + " Left");
+ writeMessage({message:['System', name + " Left"]});
+ refreshPersonsList();
};
- now.refreshPersonsList = function() {
- writeMessage("refreshing available users");
- $(".person").remove();
- for (var i in now.availablePersons) {
- writeMessage(now.availablePersons[i]);
- $("#availablePersons").append("<div class='person'>" + now.availablePersons[i] + "</div>");
- }
+ now.bufferedMessages = function(obj) {
+ for (var j in obj.buffer) writeChat(obj.buffer[j]);
};
-
+ now.refreshAvailablePersons = function(obj) {
+ $(".user").remove();
+ for (var i = 0; i < obj.users.length; i++) {
+ $("#users").prepend("<div class='user'>" + obj.users[i] + "</div>")
+ }
+ };
});
View
6 views/now/index.jade
@@ -1,12 +1,10 @@
h2 now.js sample
div#information
- span
- a(href='/') Home
div
span enter your name
form(action="javascript:setName()")
- input(type="text", id="visitorName", size="20", class="text")
- input(type="submit",value="Set Name")
+ input(type="text", id="visitorName", size="20", class="text", value=loginName)
+ input(type="submit", id="setNameButton", value="Set Name")
div
span Enter a message to be echoed
form(action="javascript:sendMessage()")
Please sign in to comment.
Something went wrong with that request. Please try again.