* Handle the reconnections to the kiwi server\r
*/\r
(function () {\r
+ // 0 = non-reconnecting state. 1 = reconnecting state.\r
var gw_stat = 0;\r
+\r
+ // If the current or upcoming disconnect was planned\r
var unplanned_disconnect = false;\r
\r
gw.on('disconnect', function (event) {\r
that.message.text(msg, {timeout: 5000});\r
}\r
\r
- // Mention the disconnection on every channel\r
+ // Mention the re-connection on every channel\r
_kiwi.app.connections.forEach(function(connection) {\r
connection.panels.server.addMsg('', msg, 'action join');\r
\r
\r
\r
gw.on('kiwi:jumpserver', function (data) {\r
- // Switching kiwi server?\r
- if (typeof data.kiwi_server !== 'undefined') {\r
- _kiwi.app.kiwi_server = data.kiwi_server;\r
- _kiwi.gateway.set('kiwi_server', data.kiwi_server);\r
- }\r
+ var serv;\r
+ // No server set? Then nowhere to jump to.\r
+ if (typeof data.kiwi_server === 'undefined')\r
+ return;\r
+\r
+ serv = data.kiwi_server;\r
+\r
+ // Strip any trailing slash from the end\r
+ if (serv[serv.length-1] === '/')\r
+ serv = serv.substring(0, serv.length-1);\r
+\r
+ _kiwi.app.kiwi_server = serv;\r
\r
// Force the jumpserver now?\r
if (data.force) {\r
// Get an interval around 1 minute so everyone doesn't reconnect it all at once\r
var jump_server_interval = Math.random() * (90 - 60) + 60;\r
\r
+ // Tell the user we are going to disconnect, wait a minute then do the actual reconnect\r
var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_prepare').fetch();\r
that.message.text(msg, {timeout: 10000});\r
\r
that.message.text(msg, {timeout: 8000});\r
\r
setTimeout(function forcedReconnectPartTwo() {\r
- _kiwi.gateway.reconnect();\r
+ _kiwi.gateway.reconnect(function() {\r
+ // Reconnect all the IRC connections\r
+ that.connections.forEach(function(con){ con.reconnect(); });\r
+ });\r
}, 5000);\r
\r
}, jump_server_interval * 1000);\r
\r
\r
this.reconnect = function (callback) {\r
+ var that = this,\r
+ transport_path;\r
+\r
this.disconnect_requested = true;\r
this.socket.disconnect();\r
- this.connect(callback);\r
+\r
+ // To get around the allow-origin issues for requests, completely reload the\r
+ // transport source from the new server\r
+ window.io = null;\r
+\r
+ // Path to get the socket.io transport code\r
+ transport_path = _kiwi.app.kiwi_server + _kiwi.app.get('base_path') + '/transport/socket.io.js?ts='+(new Date().getTime());\r
+ $script(transport_path, function() {\r
+ if (!window.io) {\r
+ return callback('err_kiwi_server_not_found');\r
+ }\r
+\r
+ that.set('kiwi_server', _kiwi.app.kiwi_server + '/kiwi');\r
+ that.connect(callback);\r
+ });\r
};\r
\r
\r
};\r
\r
\r
-\r
+ /**\r
+ * Return a new network object with the new connection details\r
+ */\r
this.newConnection = function(connection_info, callback_fn) {\r
- var that = this,\r
- h = connection_info;\r
+ var that = this;\r
\r
- this.socket.emit('kiwi', {command: 'connect', nick: h.nick, hostname: h.host, port: h.port, ssl: h.ssl, password: h.password}, function (err, server_num) {\r
+ this.makeIrcConnection(connection_info, function(err, server_num) {\r
var connection;\r
\r
if (!err) {\r
if (!_kiwi.app.connections.getByConnectionId(server_num)){\r
- connection = new _kiwi.model.Network({connection_id: server_num, nick: h.nick});\r
+ var inf = {\r
+ connection_id: server_num,\r
+ nick: connection_info.nick,\r
+ address: connection_info.host,\r
+ port: connection_info.port,\r
+ ssl: connection_info.ssl,\r
+ password: connection_info.password\r
+ };\r
+ connection = new _kiwi.model.Network(inf);\r
_kiwi.app.connections.add(connection);\r
}\r
\r
- console.log("_kiwi.gateway.socket.on('connect')");\r
+ console.log("_kiwi.gateway.socket.on('connect')", connection);\r
callback_fn && callback_fn(err, connection);\r
\r
} else {\r
});\r
};\r
\r
+\r
+ /**\r
+ * Make a new IRC connection and return its connection ID\r
+ */\r
+ this.makeIrcConnection = function(connection_info, callback_fn) {\r
+ var server_info = {\r
+ command: 'connect',\r
+ nick: connection_info.nick,\r
+ hostname: connection_info.host,\r
+ port: connection_info.port,\r
+ ssl: connection_info.ssl,\r
+ password: connection_info.password\r
+ };\r
+\r
+ this.socket.emit('kiwi', server_info, function (err, server_num) {\r
+ if (!err) {\r
+ callback_fn && callback_fn(err, server_num);\r
+\r
+ } else {\r
+ callback_fn && callback_fn(err);\r
+ }\r
+ });\r
+ };\r
+\r
+\r
this.isConnected = function () {\r
return this.socket.socket.connected;\r
};\r
*/
address: '',
+ /**
+ * The port for the network
+ * @type Int
+ */
+ port: 6667,
+
+ /**
+ * If this network uses SSL
+ * @type Bool
+ */
+ ssl: false,
+
+ /**
+ * The password to connect to this network
+ * @type String
+ */
+ password: '',
+
/**
* The current nickname
* @type String
initialize: function () {
- this.gateway = _kiwi.global.components.Network(this.get('connection_id'));
- this.bindGatewayEvents();
+ // If we already have a connection, bind our events
+ if (typeof this.get('connection_id') !== 'undefined') {
+ this.gateway = _kiwi.global.components.Network(this.get('connection_id'));
+ this.bindGatewayEvents();
+ }
// Create our panel list (tabs)
this.panels = new _kiwi.model.PanelList([], this);
},
+ reconnect: function(callback_fn) {
+ var that = this,
+ server_info = {
+ nick: this.get('nick'),
+ host: this.get('address'),
+ port: this.get('port'),
+ ssl: this.get('ssl'),
+ password: this.get('password')
+ };
+
+ _kiwi.gateway.makeIrcConnection(server_info, function(err, connection_id) {
+ if (!err) {
+ that.gateway.dispose();
+
+ that.set('connection_id', connection_id);
+ that.gateway = _kiwi.global.components.Network(that.get('connection_id'));
+ that.bindGatewayEvents();
+
+ callback_fn && callback_fn(err);
+
+ } else {
+ console.log("_kiwi.gateway.socket.on('error')", {reason: err});
+ callback_fn && callback_fn(err);
+ }
+ });
+ },
+
+
bindGatewayEvents: function () {
//this.gateway.on('all', function() {console.log('ALL', this.get('connection_id'), arguments);});
var that = this,
panels = [];
- // Multiple channels may come as comma-delimited
+ // Multiple channels may come as comma-delimited
if (typeof channels === 'string') {
channels = channels.split(',');
}
});
-
+
function onDisconnect(event) {
$.each(this.panels.models, function (index, panel) {
panel.addMsg('', _kiwi.global.i18n.translate('client_models_network_disconnected').fetch(), 'action quit');
members.remove(user, part_options);
if (part_options.current_user_kicked) {
- members.reset([]);
+ members.reset([]);
}
}