2 * Copyright (C) 2015-2020 "IoT.bzh"
3 * Author: José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 AFB = function(base, initialtoken){
19 if (typeof base != "object")
20 base = { base: base, token: initialtoken };
23 base: base.base || "api",
24 token: initialtoken || base.token
25 || URLSearchParams(window.location.search).get('access_token')
26 || URLSearchParams(window.location.search).get('token')
28 host: base.host || window.location.host,
29 url: base.url || undefined
32 var urlws = initial.url || "ws://"+initial.host+"/"+initial.base;
34 /*********************************************/
36 /**** AFB_context ****/
38 /*********************************************/
42 var TOKEN = initial.token;
44 var context = function(token, uuid) {
50 get token() {return TOKEN;},
51 set token(tok) {if(tok) TOKEN=tok;},
52 get uuid() {return UUID;},
53 set uuid(id) {if(id) UUID=id;}
56 AFB_context = new context();
58 /*********************************************/
60 /**** AFB_websocket ****/
62 /*********************************************/
70 var PROTO1 = "x-afb-ws-json1";
72 AFB_websocket = function(on_open, on_abort) {
73 var u = urlws, p = '?';
74 if (AFB_context.token) {
75 u = u + '?x-afb-token=' + AFB_context.token;
79 u = u + p + 'x-afb-uuid=' + AFB_context.uuid;
80 this.ws = new WebSocket(u, [ PROTO1 ]);
85 this.ws.onopen = onopen.bind(this);
86 this.ws.onerror = onerror.bind(this);
87 this.ws.onclose = onclose.bind(this);
88 this.ws.onmessage = onmessage.bind(this);
89 this.onopen = on_open;
90 this.onabort = on_abort;
93 function onerror(event) {
100 this.onerror && this.onerror(this);
103 function onopen(event) {
110 function onclose(event) {
114 status: 'disconnected',
115 info: 'server hung up'
118 for (var id in this.pendings) {
119 try { this.pendings[id][1](err); } catch (x) {/*NOTHING*/}
122 this.onclose && this.onclose();
125 function fire(awaitens, name, data) {
126 var a = awaitens[name];
128 a.forEach(function(handler){handler(data, name);});
129 var i = name.indexOf("/");
131 a = awaitens[name.substring(0,i)];
133 a.forEach(function(handler){handler(data, name);});
137 a.forEach(function(handler){handler(data, name);});
140 function reply(pendings, id, ans, offset) {
141 if (id in pendings) {
142 var p = pendings[id];
144 try { p[offset](ans); } catch (x) {/*TODO?*/}
148 function onmessage(event) {
149 var obj = JSON.parse(event.data);
153 AFB_context.token = obj[3];
156 reply(this.pendings, id, ans, 0);
159 reply(this.pendings, id, ans, 1);
163 fire(this.awaitens, id, ans);
175 this.onabort = function(){};
178 function call(method, request, callid) {
179 return new Promise((function(resolve, reject){
183 if (id in this.pendings)
184 throw new Error("pending callid("+id+") exists");
187 id = String(this.counter = 4095 & (this.counter + 1));
188 } while (id in this.pendings);
190 this.pendings[id] = [ resolve, reject ];
191 arr = [CALL, id, method, request ];
192 if (AFB_context.token) arr.push(AFB_context.token);
193 this.ws.send(JSON.stringify(arr));
197 function onevent(name, handler) {
199 var list = this.awaitens[id] || (this.awaitens[id] = []);
203 AFB_websocket.prototype = {
209 /*********************************************/
213 /*********************************************/
215 context: AFB_context,