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 ctx = ctx || new AfbCtxItf();
31 var wl = window.location;
32 var u = "ws://"+wl.host+"/"+base;
34 u = u + '?x-afb-token=' + ctx.token;
36 u = u + '&x-afb-uuid=' + ctx.uuid;
38 this.ws = new (WebSocket || MozWebSocket)(u, [ "x-afb-ws-json1" ]);
42 this.ws.onopen = onopen.bind(this);
43 this.ws.onerror = onerror.bind(this);
44 this.ws.onclose = onclose.bind(this);
45 this.ws.onmessage = onmessage.bind(this);
47 this.onabort = onabort;
50 function onerror(event) {
57 this.onerror && this.onerror(this);
60 function onopen(event) {
67 function onclose(event) {
68 for (var id in this.pendings) {
69 var ferr = this.pendings[id].onerror;
70 ferr && ferr(null, this);
73 this.onclose && this.onclose();
76 function onmessage(event) {
77 var obj = JSON.parse(event.data);
81 this.ctx.token = obj[3];
83 if (id && id in this.pendings) {
84 pend = this.pendings[id];
85 delete this.pendings[id];
89 pend && pend.onsuccess && pend.onsuccess(ans, this);
93 pend && pend.onerror && pend.onerror(ans, this);
102 function call(api, verb, request, onsuccess, onerror) {
103 var id = String(++this.counter);
104 this.pendings[id] = { onsuccess: onsuccess, onerror: onerror };
105 var arr = [CALL, id, api+"/"+verb, request ];
106 if (this.ctx.token) arr.push(this.ctx.token);
107 this.ws.send(JSON.stringify(arr));
110 AfbWsItf.prototype = {