1 AFB = function(base, initialtoken){
3 var urlws = "ws://"+window.location.host+"/"+base;
4 var urlhttp = "http://"+window.location.host+"/"+base;
6 /*********************************************/
8 /**** AFB_context ****/
10 /*********************************************/
14 var TOKEN = initialtoken;
16 var context = function(token, uuid) {
22 get token() {return TOKEN;},
23 set token(tok) {if(tok) TOKEN=tok;},
24 get uuid() {return UUID;},
25 set uuid(id) {if(id) UUID=id;}
28 AFB_context = new context();
30 /*********************************************/
32 /**** AFB_websocket ****/
34 /*********************************************/
41 var PROTO1 = "x-afb-ws-json1";
43 AFB_websocket = function(onopen, onabort) {
44 this.ws = new WebSocket(urlws, [ PROTO1 ]);
47 this.ws.onopen = onopen.bind(this);
48 this.ws.onerror = onerror.bind(this);
49 this.ws.onclose = onclose.bind(this);
50 this.ws.onmessage = onmessage.bind(this);
52 this.onabort = onabort;
55 function onerror(event) {
62 this.onerror && this.onerror(this);
65 function onopen(event) {
72 function onclose(event) {
73 for (var id in this.pendings) {
74 var ferr = this.pendings[id].onerror;
75 ferr && ferr(null, this);
78 this.onclose && this.onclose();
81 function onmessage(event) {
82 var obj = JSON.parse(event.data);
86 AFB_context.token = obj[3];
88 if (id && id in this.pendings) {
89 pend = this.pendings[id];
90 delete this.pendings[id];
94 pend && pend.onsuccess && pend.onsuccess(ans, this);
98 pend && pend.onerror && pend.onerror(ans, this);
107 function call(api, verb, request, onsuccess, onerror) {
108 var id = String(++this.counter);
109 this.pendings[id] = { onsuccess: onsuccess, onerror: onerror };
110 var arr = [CALL, id, api+"/"+verb, request ];
111 if (AFB_context.token) arr.push(AFB_context.token);
112 this.ws.send(JSON.stringify(arr));
115 AFB_websocket.prototype = {
120 /*********************************************/
124 /*********************************************/
126 context: AFB_context,