2 * Copyright (C) 2017, 2018 "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: base.token || initialtoken || "HELLO",
25 host: base.host || window.location.host,
26 url: base.url || undefined
29 var urlws = initial.url || "ws://"+initial.host+"/"+initial.base;
31 /*********************************************/
33 /**** AFB_context ****/
35 /*********************************************/
39 var TOKEN = initial.token;
41 var context = function(token, uuid) {
47 get token() {return TOKEN;},
48 set token(tok) {if(tok) TOKEN=tok;},
49 get uuid() {return UUID;},
50 set uuid(id) {if(id) UUID=id;}
53 AFB_context = new context();
55 /*********************************************/
57 /**** AFB_websocket ****/
59 /*********************************************/
67 var PROTO1 = "x-afb-ws-json1";
69 AFB_websocket = function(on_open, on_abort) {
70 var u = urlws, p = '?';
71 if (AFB_context.token) {
72 u = u + '?x-afb-token=' + AFB_context.token;
76 u = u + p + 'x-afb-uuid=' + AFB_context.uuid;
77 this.ws = new WebSocket(u, [ PROTO1 ]);
82 this.ws.onopen = onopen.bind(this);
83 this.ws.onerror = onerror.bind(this);
84 this.ws.onclose = onclose.bind(this);
85 this.ws.onmessage = onmessage.bind(this);
86 this.onopen = on_open;
87 this.onabort = on_abort;
90 function onerror(event) {
97 this.onerror && this.onerror(this);
100 function onopen(event) {
107 function onclose(event) {
111 status: 'disconnected',
112 info: 'server hung up'
115 for (var id in this.pendings) {
116 try { this.pendings[id][1](err); } catch (x) {/*NOTHING*/}
119 this.onclose && this.onclose();
122 function fire(awaitens, name, data) {
123 var a = awaitens[name];
125 a.forEach(function(handler){handler(data);});
126 var i = name.indexOf("/");
128 a = awaitens[name.substring(0,i)];
130 a.forEach(function(handler){handler(data);});
134 a.forEach(function(handler){handler(data);});
137 function reply(pendings, id, ans, offset) {
138 if (id in pendings) {
139 var p = pendings[id];
141 try { p[offset](ans); } catch (x) {/*TODO?*/}
145 function onmessage(event) {
146 var obj = JSON.parse(event.data);
150 AFB_context.token = obj[3];
153 reply(this.pendings, id, ans, 0);
156 reply(this.pendings, id, ans, 1);
160 fire(this.awaitens, id, ans);
172 this.onabort = function(){};
175 function call(method, request, callid) {
176 return new Promise((function(resolve, reject){
180 if (id in this.pendings)
181 throw new Error("pending callid("+id+") exists");
184 id = String(this.counter = 4095 & (this.counter + 1));
185 } while (id in this.pendings);
187 this.pendings[id] = [ resolve, reject ];
188 arr = [CALL, id, method, request ];
189 if (AFB_context.token) arr.push(AFB_context.token);
190 this.ws.send(JSON.stringify(arr));
194 function onevent(name, handler) {
196 var list = this.awaitens[id] || (this.awaitens[id] = []);
200 AFB_websocket.prototype = {
206 /*********************************************/
210 /*********************************************/
212 context: AFB_context,