2 AfbCtxItf = (function(){
7 function AfbCtxItf(token, uuid) {
12 AfbCtxItf.prototype = {
13 get token() {return TOKEN;},
14 set token(tok) {if(tok) TOKEN=tok;},
15 get uuid() {return UUID;},
16 set uuid(id) {if(id) UUID=id;}
23 AfbWsItf = (function(){
29 function AfbWsItf(base, onopen, onabort, ctx) {
30 var wl = window.location;
31 var u = "ws://"+wl.host+"/"+base;
32 this.ws = new (WebSocket || MozWebSocket)(u, [ "x-afb-ws-json1" ]);
35 this.ctx = ctx || new AfbCtxItf();
36 this.ws.onopen = onopen.bind(this);
37 this.ws.onerror = onerror.bind(this);
38 this.ws.onclose = onclose.bind(this);
39 this.ws.onmessage = onmessage.bind(this);
41 this.onabort = onabort;
44 function onerror(event) {
51 this.onerror && this.onerror(this);
54 function onopen(event) {
61 function onclose(event) {
62 for (var id in this.pendings) {
63 var ferr = this.pendings[id].onerror;
64 ferr && ferr(null, this);
67 this.onclose && this.onclose();
70 function onmessage(event) {
71 var obj = JSON.parse(event.data);
75 this.ctx.token = obj[3];
77 if (id && id in this.pendings) {
78 pend = this.pendings[id];
79 delete this.pendings[id];
83 pend && pend.onsuccess && pend.onsuccess(ans, this);
87 pend && pend.onerror && pend.onerror(ans, this);
96 function call(api, verb, request, onsuccess, onerror) {
97 var id = String(++this.counter);
98 this.pendings[id] = { onsuccess: onsuccess, onerror: onerror };
99 var arr = [CALL, id, api+"/"+verb, request ];
100 if (this.ctx.token) arr.push(this.ctx.token);
101 this.ws.send(JSON.stringify(arr));
104 AfbWsItf.prototype = {