connection and ping support
This commit is contained in:
parent
8f25b4c862
commit
11d236eb9f
2 changed files with 72 additions and 13 deletions
|
@ -19,20 +19,52 @@ var NsClient = function() {
|
|||
this.state = "actif";
|
||||
this.allowed_statuses = ["actif", "away", "idle", "lock"];
|
||||
this.is_connected = false;
|
||||
this.socket = null;
|
||||
};
|
||||
|
||||
NsClient.prototype.connect = function() {
|
||||
var cnt = function(elem) {
|
||||
return function(infos) {
|
||||
if (typeof infos.login !== "undefined" && typeof infos.pwd_socks !== "undefined") {
|
||||
elem.is_connected = true;
|
||||
console.info("connected to the netsoul server");
|
||||
elem.updateStatus();
|
||||
chrome.socket.create('tcp', {}, function(sock_inf) {
|
||||
elem.socket = sock_inf.socketId;
|
||||
chrome.socket.connect(elem.socket, "ns-server.epita.fr", 4242, function(res) {
|
||||
chrome.socket.read(elem.socket, null, function(rd_inf) {
|
||||
if (rd_inf.resultCode > 0) {
|
||||
var data = ab2str(rd_inf.data).split(' '),
|
||||
auth = "ext_user_log ";
|
||||
auth += infos.login + " ";
|
||||
auth += hex_md5(data[2] + "-" + data[3] + "/" + data[4] + infos.pwd_socks) + " ";
|
||||
auth += "chromesoul chromesoul\n";
|
||||
console.log("recv: " + ab2str(rd_inf.data));
|
||||
console.log("sent: auth_ag ext_user none none\n");
|
||||
chrome.socket.write(elem.socket, str2ab("auth_ag ext_user none none\n"), function(w_inf) {
|
||||
chrome.socket.read(elem.socket, null, function(rd_inf) {
|
||||
if (rd_inf.resultCode > 0) {
|
||||
console.log("recv: " + ab2str(rd_inf.data));
|
||||
console.log("sent: " + auth);
|
||||
chrome.socket.write(elem.socket, str2ab(auth), function(w_inf) {
|
||||
chrome.socket.read(elem.socket, null, function(rd_inf) {
|
||||
if (rd_inf.resultCode > 0) {
|
||||
console.log("recv: " + ab2str(rd_inf.data));
|
||||
elem.is_connected = true;
|
||||
console.info("connected to the netsoul server");
|
||||
elem.updateStatus();
|
||||
elem.daemonize();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
console.log('connecting...');
|
||||
if (!this.is_connected) {
|
||||
this.storage.get(null, cnt(this));
|
||||
} else {
|
||||
|
@ -42,9 +74,32 @@ NsClient.prototype.connect = function() {
|
|||
|
||||
NsClient.prototype.disconnect = function() {
|
||||
if (this.is_connected) {
|
||||
chrome.socket.disconnect(this.socket);
|
||||
this.is_connected = false;
|
||||
console.info("disconnected");
|
||||
this.updateStatus();
|
||||
} else {
|
||||
console.warn("not connected");
|
||||
}
|
||||
};
|
||||
|
||||
NsClient.prototype.daemonize = function() {
|
||||
var dm = function(elem) {
|
||||
return function(rd_inf) {
|
||||
if (rd_inf.resultCode > 0) {
|
||||
var data = ab2str(rd_inf.data);
|
||||
console.log("- recv: " + data);
|
||||
if (data.substr(0, 5) === "ping ") {
|
||||
console.log("- sent: " + data);
|
||||
chrome.socket.write(elem.socket, rd_inf.data, function(w_inf) {
|
||||
chrome.socket.read(elem.socket, null, this);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
if (this.is_connected) {
|
||||
chrome.socket.read(this.socket, null, dm(this));
|
||||
} else {
|
||||
console.warn("not connected");
|
||||
}
|
||||
|
@ -52,7 +107,11 @@ NsClient.prototype.disconnect = function() {
|
|||
|
||||
NsClient.prototype.updateStatus = function() {
|
||||
if (this.is_connected) {
|
||||
console.info("updating status");
|
||||
var status_msg = "user_cmd state ";
|
||||
status_msg += this.state + ":";
|
||||
status_msg += Math.round(new Date().getTime() / 1000) + "\n";
|
||||
console.log("+send: " + status_msg);
|
||||
chrome.socket.write(this.socket, str2ab(status_msg), function(w_inf) {});
|
||||
} else {
|
||||
console.warn("not connected");
|
||||
}
|
||||
|
@ -94,6 +153,6 @@ NsClient.prototype.init = function() {
|
|||
};
|
||||
|
||||
con(this).apply();
|
||||
setInterval(con(this), 60000);
|
||||
setInterval(con(this), 10000);
|
||||
setInterval(upd(this), 600000);
|
||||
};
|
||||
|
|
Reference in a new issue