ability to receive messages

This commit is contained in:
Rodolphe Breard 2012-11-26 15:31:15 +01:00
parent 096cb39320
commit ba7ef4f2b0
4 changed files with 61 additions and 24 deletions

View file

@ -48,9 +48,9 @@ var NsClient = function() {
mo.socket = mch[1]; mo.socket = mch[1];
mo.login = mch[2]; mo.login = mch[2];
mo.host = mch[3]; mo.host = mch[3];
mo.location = decodeURI(mch[4]); mo.location = elem.msgDecode(mch[4]);
mo.group = mch[5]; mo.group = mch[5];
mo.message = decodeURI(mch[6]); mo.message = elem.msgDecode(mch[6]);
elem.msgHandler(mo); elem.msgHandler(mo);
} }
@ -60,6 +60,18 @@ var NsClient = function() {
})(this); })(this);
}; };
NsClient.prototype.msgDecode = function(msg) {
msg = unescape(msg)
// TODO: unescape extra characters: @*/+
return msg;
};
NsClient.prototype.msgEncode = function(msg) {
msg = escape(msg)
// TODO: escape extra characters: @*/+
return msg;
};
NsClient.prototype.connect = function() { NsClient.prototype.connect = function() {
if (!this.is_connected) { if (!this.is_connected) {
this.storage.get(null, (function(elem) { this.storage.get(null, (function(elem) {
@ -139,21 +151,29 @@ NsClient.prototype.daemonize = function() {
} }
}; };
NsClient.prototype.sendMessage = function(to, message) {
var msg = "user_cmd msg_user " + to + " msg " + this.msgEncode(message) + "\n";
chrome.socket.write(this.socket, str2ab(msg), function(w_inf) {});
};
NsClient.prototype.updateStatus = function() { NsClient.prototype.updateStatus = function() {
var status_txt = document.getElementById("status_txt"); var status_txt = document.getElementById("status_txt");
if (this.is_connected) { if (this.is_connected) {
if (status_txt !== null) if (status_txt !== null) {
status_txt.innerHTML = this.state; status_txt.innerHTML = this.state;
}
var status_msg = "user_cmd state "; var status_msg = "user_cmd state ";
status_msg += this.state + ":"; status_msg += this.state + ":";
status_msg += Math.round(new Date().getTime() / 1000) + "\n"; status_msg += Math.round(new Date().getTime() / 1000) + "\n";
chrome.socket.write(this.socket, str2ab(status_msg), function(w_inf) {}); chrome.socket.write(this.socket, str2ab(status_msg), function(w_inf) {});
} else { } else {
if (status_txt !== null) if (status_txt !== null) {
status_txt.innerHTML = 'disconnected'; status_txt.innerHTML = 'disconnected';
} }
}
}; };
NsClient.prototype.changeStatus = function(new_status) { NsClient.prototype.changeStatus = function(new_status) {

View file

@ -133,9 +133,11 @@ Nsui.prototype.init = function() {
}; };
})(this); })(this);
if (typeof $cs.client !== "undefined") {
NsClient.prototype.msgHandler = (function(elem) { NsClient.prototype.msgHandler = (function(elem) {
return function(msg) { return function(msg) {
elem.addContentToTab(msg.login, msg); elem.addContentToTab(msg.login, msg);
}; };
})(this); })(this);
}
}; };

View file

@ -70,6 +70,20 @@ Tab.prototype.initBodyElement = function() {
chat_input_w.classList.add("chat-input-wrapper"); chat_input_w.classList.add("chat-input-wrapper");
chat_input.classList.add("chat-input"); chat_input.classList.add("chat-input");
chat_input.setAttribute("type", "text"); chat_input.setAttribute("type", "text");
chat_input.addEventListener("keyup", (function(elem) {
return function(event) {
if (event.keyCode == 13 && this.value != "") {
var msg = this.value;
this.value = "";
if (typeof $cs.client !== "undefined") {
$cs.client.sendMessage(elem.name, msg);
} else {
console.error("chromesoul client not found");
}
}
};
})(this), false);
chat_input_w.appendChild(chat_input); chat_input_w.appendChild(chat_input);
this.el_body.appendChild(this.chat_log); this.el_body.appendChild(this.chat_log);

View file

@ -1,17 +1,18 @@
(function() { var chromesoul = (function() {
var om, ui, cs; var chromesoul = {
"opts": new OptionsManager(),
"ui": new Nsui(),
"client": new NsClient()
};
om = new OptionsManager(); return (window.chromesoul = window.$cs = chromesoul);
om.init();
ui = new Nsui();
ui.init();
cs = new NsClient();
cs.init();
ui.setReconnect(function() {
cs.disconnect();
cs.connect();
});
})(); })();
$cs.opts.init();
$cs.ui.init();
$cs.client.init();
$cs.ui.setReconnect(function() {
$cs.client.disconnect();
$cs.client.connect();
});