Skip to content
Browse files

Added code to handle webuser colouring.

Minor cleanup and cosmetics.

Signed-off-by: Paul Maxan <pmaxan@yahoo.com>
  • Loading branch information...
1 parent 0636e7d commit b3f9acd5fa5af76ec4f444272852018f1b690588 Paul Maxan committed Apr 17, 2012
Showing with 94 additions and 48 deletions.
  1. +38 −19 irc.js
  2. +7 −3 public/js/app.js
  3. +16 −15 public/js/irc-client.js
  4. +18 −5 public/js/tcp-client.js
  5. +15 −6 tcp-proxy.js
View
57 irc.js
@@ -73,8 +73,10 @@ console.log("IRC#nodester is running on port %d in %s mode", app.address().port,
/*
* array of nicks that connect through the web interface
+ * and a flag signalling that a webUser refresh is needed on webclient
*/
-var webusers = [];
+var webUsers = [],
+ webUsersIsDirty = false;
/*
* app specific processor, e.g., statistics, web users
@@ -93,33 +95,50 @@ var appProcessor = function (action, client, msg, socket) {
st: startTime,
min: minMem,
max: maxMem,
- current: currMem
+ current: currMem,
+ wud: webUsersIsDirty
}));
break;
case "requestWebUsers":
- switch (msg.data) {
- case "add":
- webusers.push(msg.user);
- break;
- case "remove":
- for (var i = 0; i < webusers.length; i++) {
- if (webusers[i] == msg.user) {
- webusers.slice(i,1);
- break;
- }
+ client.send(JSON.stringify({
+ action: "webusers",
+ webUsers: webUsers
+ }));
+ webUsersIsDirty = false;
+ break
+ case "addWebUsers":
+ /*
+ * at this stage of development we cannot tell which user that was previously
+ * connected through a web interface left
+ * all we can do is make sure we do not have duplicates
+ * this in time will slow down the webclient operation
+ */
+ var found = false;
+ for (var i = 0; i < webUsers.length; i++) {
+ if (webUsers[i] == msg.data) {
+ found = true;
+ break;
}
- break;
- default: //list
- client.send(JSON.stringify({
- action: "webusers",
- webusers: webusers
- }));
}
+ if (!found) {
+ webUsers.push(msg.data);
+ webUsersIsDirty = true;
+ }
+ break;
+ case "deleteWebUsers":
+ for (var i = 0; i < webUsers.length; i++) {
+ if (webUsers[i] == msg.data) {
+ webUsers.slice(i,1);
+ webUsersIsDirty = true;
+ break;
+ }
+ }
+ break;
default:
}
break;
case "disconnect":
-
+ socket.write("QUIT :Webclient closed browser.\r\n");
break;
default:
}
View
10 public/js/app.js
@@ -370,7 +370,7 @@ $(document).ready(function() {
logBox.slideToggle();
nickLabel.text(nickname);
joinBtn.removeAttr("disabled");
- //initiate one channel join
+ //initiate single-channel join
ircClient.joinChannel(channel);
break;
case "join":
@@ -404,16 +404,20 @@ $(document).ready(function() {
}
nicks.sort(cisort);
nicksToList();
- //ircClient.requestStatistics(); this call will be needed if the web client will also send commands
+ ircClient.requestStatistics();
break;
case "statistics":
c.updateStats(obj);
+ if (obj.wud == true) {
+ //the webUsers list has been changed, we initiate retrieval
+ ircClient.requestWebUsers();
+ }
var header_class = (c.getStatsEnabled() == true) ? 'header-stats' : 'header-stats off';
nickLabel.html('<span class="'+header_class+'">Server up for ' + c.getServerTime()
+ ', using ' + c.getMinRss() + '-' + c.getMaxRss() + ' MB of RAM</span> ' + nickname);
break;
case "webusers":
- webNicks = obj.wu;
+ webNicks = obj.webusers;
nicksToList();
break;
default:
View
31 public/js/irc-client.js
@@ -58,12 +58,16 @@ IRCClient.prototype.connect = function () {
st: data.st,
min: data.min,
max: data.max,
- current: data.current
+ current: data.current,
+ wud: data.wud
}));
});
this.client.on("webusers", function (data) {
-
+ that.emit("data", JSON.stringify({
+ messagetype: "webusers",
+ webusers: data.webUsers
+ }));
});
this.client.connect();
@@ -98,19 +102,9 @@ IRCClient.prototype.requestStatistics = function () {
this.client.send("requestStatistics");
}
-IRCClient.prototype.addWebUsers = function (userNick) {
-
- this.client.send("requestWebUsers", param);
-}
-IRCClient.prototype.updateWebUsers = function (formerNick, newNick) {
-
- this.client.send("requestWebUsers", param);
+IRCClient.prototype.requestWebUsers = function () {
+ this.client.send("requestWebUsers");
}
-IRCClient.prototype.listWebUsers = function () {
-
- this.client.send("requestWebUsers", param);
-}
-
IRCClient.prototype.clearAll = function () {
this.client.clearAll();
@@ -269,6 +263,9 @@ var emulateMessage = function (that, message) {
* Handler for join
*/
case "JOIN":
+ if (that.nick == message.nick) {
+ that.client.send("addWebUsers", that.nick);
+ }
that.emit("data", JSON.stringify({
messagetype: "join",
from: (message.nick),
@@ -382,8 +379,12 @@ var emulateMessage = function (that, message) {
};
};
*/
- if (that.nick == prevNick)
+ if (that.nick == prevNick) {
that.nick = newNick;
+ //perform an "update" of web users
+ that.client.send("deleteWebUsers", prevNick);
+ that.client.send("addWebUsers", newNick);
+ }
that.emit("data", JSON.stringify({
messagetype: "nick",
from: prevNick,
View
23 public/js/tcp-client.js
@@ -20,20 +20,28 @@ Client.prototype.connect = function () {
if (typeof this.socket === "undefined" || this.socket === null) {
this.socket = io.connect("http://" + window.location.host, {"reconnect": false});
} else {
- //these are retries or reconnects
+ //reconnects
if (this.socket.socket.connected) {
that.socket.removeAllListeners("connect");
that.socket.removeAllListeners("disconnect");
that.socket.removeAllListeners("message");
- that.socket.send(JSON.stringify({action: "connect", host: that.host, port: that.port}));
+ that.socket.send(JSON.stringify({
+ action: "connect",
+ host: that.host,
+ port: that.port
+ }));
} else {
this.socket.socket.reconnect();
}
}
this.socket.on("connect", function () {
that.sioIsConnected = true;
- that.socket.send(JSON.stringify({action: "connect", host: that.host, port: that.port}));
+ that.socket.send(JSON.stringify({
+ action: "connect",
+ host: that.host,
+ port: that.port
+ }));
});
this.socket.on("disconnect", function () {
@@ -66,7 +74,9 @@ Client.prototype.connect = function () {
Client.prototype.disconnect = function () {
if (this.sioIsConnected)
- this.socket.send(JSON.stringify({action: "disconnect"}));
+ this.socket.send(JSON.stringify({
+ action: "disconnect"
+ }));
}
/*
@@ -75,7 +85,10 @@ Client.prototype.disconnect = function () {
*/
Client.prototype.send = function (action, data) {
if (this.sioIsConnected && this.endpointIsConnected) {
- this.socket.send(JSON.stringify({action: action, data: data}));
+ this.socket.send(JSON.stringify({
+ action: action,
+ data: data
+ }));
}
}
View
21 tcp-proxy.js
@@ -16,20 +16,27 @@ var Proxy = function (client, appProcessor) {
socket.on("end", function () {
socketIsConnected = false;
if (clientIsConnected)
- client.send(JSON.stringify({action: "closed"}));
+ client.send(JSON.stringify({
+ action: "closed"
+ }));
});
socket.on("connect", function () {
socketIsConnected = true;
console.log("socket connected");
if (clientIsConnected)
- client.send(JSON.stringify({action: "connected"}));
+ client.send(JSON.stringify({
+ action: "connected"
+ }));
});
socket.on("data", function (data) {
console.log("RECV:" + data + ", length: " + data.length );
if (clientIsConnected)
- client.send(JSON.stringify({action: "data", data: data}));
+ client.send(JSON.stringify({
+ action: "data",
+ data: data
+ }));
});
//client related
@@ -45,10 +52,12 @@ var Proxy = function (client, appProcessor) {
if (typeof appProcessor === "function") {
appProcessor("disconnect", null, null, socket);
}
- socketIsConnected = false;
- socket.destroy();
+ setTimeout(function () {
+ socketIsConnected = false;
+ socket.destroy();
+ console.log("client disconnected");
+ }, 1000);
}
- console.log("client disconnected");
});
client.on("message", function (message) {

0 comments on commit b3f9acd

Please sign in to comment.
Something went wrong with that request. Please try again.