Simpler protocol (note how the "server" doesn't change!)
This commit is contained in:
parent
9da480ed0a
commit
8c914f93d6
34
index.js
34
index.js
|
@ -276,6 +276,14 @@ WebSocketConnection.prototype.onclose = function (e) {
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
// Main
|
// Main
|
||||||
|
|
||||||
|
function chatEvent(nym, status, utterance, stamp) {
|
||||||
|
return ["chatEvent", nym, status, utterance, stamp || +(new Date())];
|
||||||
|
}
|
||||||
|
function chatEventNym(c) { return c[1]; }
|
||||||
|
function chatEventStatus(c) { return c[2]; }
|
||||||
|
function chatEventUtterance(c) { return c[3]; }
|
||||||
|
function chatEventStamp(c) { return c[4]; }
|
||||||
|
|
||||||
function outputItem(item) {
|
function outputItem(item) {
|
||||||
var stamp = $("<span/>").text((new Date()).toGMTString()).addClass("timestamp");
|
var stamp = $("<span/>").text((new Date()).toGMTString()).addClass("timestamp");
|
||||||
var item = $("<div/>").append([stamp].concat(item));
|
var item = $("<div/>").append([stamp].concat(item));
|
||||||
|
@ -286,17 +294,16 @@ function outputItem(item) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateNymList(rs) {
|
function updateNymList(rs) {
|
||||||
var nyms = [];
|
|
||||||
var statuses = {};
|
var statuses = {};
|
||||||
for (var i = 0; i < rs.length; i++) {
|
for (var i = 0; i < rs.length; i++) {
|
||||||
var p = rs[i].pattern;
|
var p = rs[i].pattern;
|
||||||
if (p[0] === "broker" && p[1] === 0 && p[2][1] === "says") {
|
if (p[0] === "broker" && p[1] === 0 && p[2][0] === "chatEvent") {
|
||||||
nyms.push(p[2][0]);
|
statuses[chatEventNym(p[2])] = chatEventStatus(p[2]);
|
||||||
}
|
|
||||||
if (p[0] === "broker" && p[1] === 0 && p[2][1] === "status") {
|
|
||||||
statuses[p[2][0]] = p[2][2];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
var nyms = [];
|
||||||
|
for (var nym in statuses) { nyms.push(nym); }
|
||||||
|
nyms.sort();
|
||||||
|
|
||||||
var container = $("#nymlist");
|
var container = $("#nymlist");
|
||||||
container[0].innerHTML = ""; // remove all children
|
container[0].innerHTML = ""; // remove all children
|
||||||
|
@ -367,10 +374,8 @@ $(document).ready(function () {
|
||||||
sub(["jQuery", "#nym", "change", __]),
|
sub(["jQuery", "#nym", "change", __]),
|
||||||
sub(["jQuery", "#status", "change", __]),
|
sub(["jQuery", "#status", "change", __]),
|
||||||
sub(["jQuery", "#wsurl", "change", __]),
|
sub(["jQuery", "#wsurl", "change", __]),
|
||||||
pub(["broker", 0, [this.nym(), "says", __]]),
|
pub(["broker", 0, chatEvent(this.nym(), this.currentStatus(), __, __)]),
|
||||||
pub(["broker", 0, [this.nym(), "status", this.currentStatus()]]),
|
sub(["broker", 0, chatEvent(__, __, __, __)], 0, 1)];
|
||||||
sub(["broker", 0, [__, "says", __]], 0, 1),
|
|
||||||
sub(["broker", 0, [__, "status", __]], 0, 1)];
|
|
||||||
},
|
},
|
||||||
handleEvent: function (e) {
|
handleEvent: function (e) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
@ -391,7 +396,9 @@ $(document).ready(function () {
|
||||||
var utterance = inp.val();
|
var utterance = inp.val();
|
||||||
inp.val("");
|
inp.val("");
|
||||||
if (utterance) {
|
if (utterance) {
|
||||||
World.send(["broker", 0, [this.nym(), "says", utterance]]);
|
World.send(["broker", 0, chatEvent(this.nym(),
|
||||||
|
this.currentStatus(),
|
||||||
|
utterance)]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "#nym":
|
case "#nym":
|
||||||
|
@ -408,8 +415,9 @@ $(document).ready(function () {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "broker":
|
case "broker":
|
||||||
if (e.message[2][1] === "says") {
|
if (e.message[2][0] === "chatEvent") {
|
||||||
outputUtterance(e.message[2][0], e.message[2][2]);
|
outputUtterance(chatEventNym(e.message[2]),
|
||||||
|
chatEventUtterance(e.message[2]));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Reference in New Issue