spongy-client-chrome/app/server.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

2014-10-24 16:56:40 -06:00
// Functionality dealing with server-level things
var maxScrollback = 500;
2014-10-24 23:25:22 -06:00
function Server(network, baseURL, authtok, messageHandler) {
2014-10-24 16:56:40 -06:00
function handleEventSourceLine(line) {
var lhs = line.split(" :", 1)[0]
var parts = lhs.split(' ')
var timestamp = new Date(parts[0] * 1000);
var fullSender = parts[1];
var command = parts[2];
var sender = parts[3];
var forum = parts[4];
var args = parts.slice(5);
var txt = line.substr(lhs.length + 2);
messageHandler(timestamp, fullSender, command, sender, forum, args, txt);
}
function handleEventSourceMessage(oEvent) {
msgs = oEvent.data.split("\n");
2014-10-24 23:25:22 -06:00
var first = Math.max(0, msgs.length - maxScrollback);
2014-10-24 16:56:40 -06:00
for (var i = first; i < msgs.length; i += 1) {
2014-10-24 23:25:22 -06:00
handleEventSourceLine(msgs[i]);
2014-10-24 16:56:40 -06:00
}
}
function handleEventSourceError(oEvent) {
timestamp = new Date();
messageHandler(timestamp, null, "ERROR", null, null, [], null);
}
2014-10-27 10:23:50 -06:00
function send(target, text) {
function handleError(oEvent) {
console.log("XXX: That didn't work out.", target, text)
}
var form = new FormData();
form.append("type", "command");
form.append("auth", authtok);
form.append("network", network);
form.append("target", target);
form.append("text", text);
console.log(form);
var oReq = new XMLHttpRequest();
oReq.addEventListener("error", handleError);
oReq.open("POST", baseURL, true);
oReq.send(form);
}
this.send = send;
2014-10-24 23:25:22 -06:00
var pullURL = baseURL + "?network=" + encodeURIComponent(network) + "&auth=" + encodeURIComponent(authtok);
2014-10-27 10:23:50 -06:00
var eventSource = new EventSource(pullURL);
eventSource.addEventListener("message", handleEventSourceMessage);
eventSource.addEventListener("error", handleEventSourceError);
2014-10-24 16:56:40 -06:00
}