Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed reconnect after a failed connect.

  • Loading branch information...
commit 7aacf5abded88939965529ac840f7771c86e07d8 1 parent 8f873c1
@microp11 microp11 authored
View
20 irc.js
@@ -19,9 +19,9 @@
* : express with a static http file server
*/
-var express = require('express'),
- sio = require('socket.io'),
- Proxy = require('./tcp-proxy');
+var express = require("express"),
+ sio = require("socket.io"),
+ Proxy = require("./tcp-proxy");
var app = module.exports = express.createServer();
@@ -34,7 +34,7 @@ var allowCORS = function (req, res, next) {
};
app.configure(function (){
- app.use(express.static(__dirname + '/public'));
+ app.use(express.static(__dirname + "/public"));
app.use(allowCORS);
});
@@ -60,19 +60,19 @@ var tmr = setInterval(function () {
console.log(statTime, "curr:", currMem, "min:", minMem, "max: ", maxMem);
}, 15*1000);
-process.on('uncaughtException', function (err) {
- console.log('Uncaught error: ' + err.stack);
+process.on("uncaughtException", function (err) {
+ console.log("Uncaught error: " + err.stack);
});
-app.get('/', function (req, res, next) {
- res.render('./public/index.html');
+app.get("/", function (req, res, next) {
+ res.render("./public/index.html");
});
app.listen(process.env.C9_PORT || process.env['app_port'] || 16960);
console.log("IRC#nodester is running on port %d in %s mode", app.address().port, app.settings.env);
var io = sio.listen(app);
-io.set('log level', 0);
-io.sockets.on('connection', function (socket) {
+//io.set('log level', 0);
+io.sockets.on("connection", function (socket) {
var proxy = new Proxy(socket);
});
View
17 public/js/app.js
@@ -126,6 +126,8 @@ $(document).ready(function() {
ircClient.clearAll();
ircClient.on("connected", handleOnConnected);
ircClient.on("disconnected", handleOnDisconnected);
+ ircClient.on("closed", handleOnClosed);
+ ircClient.on("error", handleOnError);
ircClient.on("data", handleOnData);
ircClient.connect();
} else {
@@ -453,6 +455,21 @@ $(document).ready(function() {
}, 1000);
};
+ /*
+ * TODO
+ * These messages and actions are to be refined later once we cover
+ * the entire existing functionality
+ */
+ var handleOnClosed = function () {
+ appendEvent("*", "disconnected", false);
+ nicks = [];
+ nicksToList();
+ };
+
+ var handleOnError = function () {
+ appendEvent("*", "error", false);
+ };
+
var sendMessage = function () {
appendMessage(nickname, textInput.val(), true);
ircClient.sendPrivMsg(textInput.val());
View
19 public/js/irc-client.js
@@ -25,10 +25,18 @@ IRCClient.prototype.connect = function () {
that.emit("connected");
});
- this.client.on("closed", function () {
+ this.client.on("disconnected", function () {
that.emit("disconnected");
});
+ this.client.on("closed", function () {
+ that.emit("closed");
+ });
+
+ this.client.on("error", function () {
+ that.emit("error");
+ });
+
this.client.on("data", function (chunk) {
that.buffer += chunk.data;
var lines = that.buffer.split("\r\n");
@@ -58,6 +66,12 @@ IRCClient.prototype.joinChannel = function (channel) {
this.client.send("JOIN " + channel + "\r\n");
}
+IRCClient.prototype.quit = function (reason) {
+ reason = reason || "session closed";
+ this.channel = channel;
+ this.client.send("QUIT :" + reason + "\r\n");
+}
+
IRCClient.prototype.disconnect = function () {
this.client.disconnect();
}
@@ -66,6 +80,7 @@ IRCClient.prototype.sendPrivMsg = function (message) {
this.client.send("PRIVMSG " + this.channel + " :" + message + "\r\n");
}
+//TODO make this private
IRCClient.prototype.send = function (message) {
this.client.send(message);
}
@@ -79,6 +94,7 @@ IRCClient.prototype.requestWebUsers = function () {
}
IRCClient.prototype.clearAll = function () {
+ this.client.clearAll();
this.callbacks = {};
}
@@ -289,7 +305,6 @@ var emulateMessage = function (that, message) {
/*
* Handler for quitting
- * This event will not be triggered after an irc.quit() call
*/
case "QUIT":
that.emit("data", JSON.stringify({
View
32 public/js/tcp-client.js
@@ -8,7 +8,7 @@
var Client = function (host, port) {
this.host = host;
this.port = port;
- this.webUAIsConnected = false;
+ this.sioIsConnected = false;
this.endpointIsConnected = false;
this.callbacks = {};
return this;
@@ -18,24 +18,28 @@ Client.prototype.connect = function () {
var that = this;
if (typeof this.socket === "undefined" || this.socket === null) {
- this.socket = io.connect("http://" + window.location.host, {"reconnect": false})
+ this.socket = io.connect("http://" + window.location.host, {"reconnect": false});
} else {
+ //these are retries or reconnects
if (this.socket.socket.connected) {
- //TODO
+ 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}));
} else {
- socket.socket.reconnect();
+ this.socket.socket.reconnect();
}
}
this.socket.on("connect", function () {
- that.webUAIsConnected = true;
+ that.sioIsConnected = true;
that.socket.send(JSON.stringify({action: "connect", host: that.host, port: that.port}));
});
this.socket.on("disconnect", function () {
- that.webUAIsConnected = false;
+ that.sioIsConnected = false;
that.endpointIsConnected = false;
- that.emit("error", "the socket connection to UA was lost");
+ that.emit("disconnected"); //the socket connection was lost
});
this.socket.on("message", function (data) {
@@ -45,12 +49,12 @@ Client.prototype.connect = function () {
that.endpointIsConnected = true;
that.emit("connected");
break;
+ case "closed":
+ that.endpointIsConnected = false;
+ that.emit("closed"); //the proxy lost connection to irc server
case "data":
that.emit("data", {data: data.data});
break;
- case "closed":
- that.endpointIsConnected = false;
- that.emit("closed");
default:
}
});
@@ -59,12 +63,12 @@ Client.prototype.connect = function () {
}
Client.prototype.disconnect = function () {
- if (this.webUAIsConnected)
+ if (this.sioIsConnected)
this.socket.send(JSON.stringify({action: "disconnect"}));
}
Client.prototype.send = function (data1) {
- if (this.webUAIsConnected && this.endpointIsConnected) {
+ if (this.sioIsConnected && this.endpointIsConnected) {
this.socket.send(JSON.stringify({action: "data", data: data1}));
}
}
@@ -78,4 +82,8 @@ Client.prototype.on = function (event, callback) {
if (typeof callback === "function")
this.callbacks[event] = callback;
return this;
+}
+
+Client.prototype.clearAll = function () {
+ this.callbacks = {};
}
View
8 tcp-proxy.js
@@ -27,7 +27,7 @@ var Proxy = function (client) {
});
socket.on("data", function (data) {
- console.log("data arrived:" + data + ", length: " + data.length );
+ console.log("RECV:" + data + ", length: " + data.length );
if (clientIsConnected)
client.send(JSON.stringify({action: "data", data: data}));
});
@@ -47,16 +47,16 @@ var Proxy = function (client) {
msg = JSON.parse(message);
switch (msg.action) {
case "connect":
- console.log("connect request received: " + msg.host + ":" + msg.port);
+ console.log("CONNECT: " + msg.host + ":" + msg.port);
socket.connect(msg.port, msg.host);
break;
case "disconnect":
- console.log("disconnect request received");
+ console.log("DISCONNECT");
if (socketIsConnected)
socket.end();
break;
case "data":
- console.log("data request received");
+ console.log("SEND");
if (socketIsConnected) {
console.log(msg.data);
socket.write(msg.data);
Please sign in to comment.
Something went wrong with that request. Please try again.