This repository has been archived on 2023-09-20. You can view files and clone it, but cannot push or open issues or pull requests.
chromesoul/lib/client.js

138 lines
3.7 KiB
JavaScript
Raw Permalink Normal View History

2013-02-27 15:37:08 +01:00
//
// Copyright (c) 2013 Rodolphe Breard
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
var Client = function() {
this.socket = null;
this.is_connected = false;
this.client = null;
this.ui = null;
this.status = null;
this.contacts = [];
2013-03-01 17:44:34 +01:00
this.waiting_contacts = [];
2013-02-27 15:37:08 +01:00
};
Client.prototype.createSocket = function() {
if (this.socket === null) {
2014-11-06 18:52:14 +01:00
this.socket = new TxtSocket();
this.socket.onError = (function(elem) {
return function() {
elem.is_connected = false;
elem.connect();
};
})(this);
2013-02-27 15:37:08 +01:00
}
};
Client.prototype.connect = function() {
if (!this.is_connected) {
2014-11-06 18:52:14 +01:00
this.createSocket();
this.socket.connect(this.client.host, this.client.port, (function(elem) {
return function() {
elem.client.connect(elem, function() {
if (elem.is_connected) {
elem.daemonize();
elem.changeStatus();
elem.addContact();
} else {
elem.disconnect();
}
});
};
})(this));
2013-02-27 15:37:08 +01:00
}
}
Client.prototype.disconnect = function() {
if (this.is_connected) {
2014-11-06 18:52:14 +01:00
this.is_connected = false;
this.client.disconnect(this);
this.socket.disconnect();
this.socket = null;
2013-02-27 15:37:08 +01:00
}
this.changeStatus(this.client.status_disconnected);
};
Client.prototype.reconnect = function() {
this.disconnect();
this.connect();
};
Client.prototype.daemonize = function() {
this.socket.read((function(elem) {
2014-11-06 18:52:14 +01:00
return function(data) {
elem.client.recv(elem, data);
elem.daemonize();
};
2013-02-27 15:37:08 +01:00
})(this));
};
Client.prototype.changeStatus = function(status) {
if (typeof status === 'undefined') {
2014-11-06 18:52:14 +01:00
status = this.client.default_status;
2013-02-27 15:37:08 +01:00
}
if (status !== this.status) {
2014-11-06 18:52:14 +01:00
this.status = status;
this.updateStatus();
2013-02-27 15:37:08 +01:00
}
};
Client.prototype.updateStatus = function() {
if (this.status !== null) {
2014-11-06 18:52:14 +01:00
this.client.changeStatus(this);
2013-02-27 15:37:08 +01:00
}
};
Client.prototype.addContact = function(name) {
2013-03-01 17:44:34 +01:00
if (typeof name !== 'undefined') {
2014-11-06 18:52:14 +01:00
this.waiting_contacts.push(name);
2013-03-01 17:44:34 +01:00
}
2013-02-27 15:37:08 +01:00
if (this.is_connected) {
2014-11-06 18:52:14 +01:00
var tmp = this.waiting_contacts;
this.waiting_contacts = [];
this.client.addContact(this, tmp);
2013-02-27 15:37:08 +01:00
}
};
Client.prototype.rmContact = function(name) {
if (this.is_connected) {
2014-11-06 18:52:14 +01:00
this.client.rmContact(this, name);
2013-02-27 15:37:08 +01:00
}
};
Client.prototype.speak = function(to, msg) {
if (this.is_connected) {
2014-11-06 18:52:14 +01:00
this.client.speak(this, to, msg);
2013-02-27 15:37:08 +01:00
}
};
Client.prototype.init = function(client, ui) {
this.client = client;
this.ui = ui;
this.connect();
setInterval((function(elem) {
2014-11-06 18:52:14 +01:00
return function() {
elem.connect();
};
2013-02-27 15:37:08 +01:00
})(this), 10000);
2013-03-18 10:45:24 +01:00
setInterval((function(elem) {
2014-11-06 18:52:14 +01:00
return function() {
elem.updateStatus();
};
2013-03-18 10:45:24 +01:00
})(this), 180000);
2013-02-27 15:37:08 +01:00
};