2 * Copyright (C) 2017 "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(onopen, onabort) {
71 if (AFB_context.token) {
72 u = u + '?x-afb-token=' + AFB_context.token;
74 u = u + '&x-afb-uuid=' + AFB_context.uuid;
76 this.ws = new WebSocket(u, [ PROTO1 ]);
81 this.ws.onopen = onopen.bind(this);
82 this.ws.onerror = onerror.bind(this);
83 this.ws.onclose = onclose.bind(this);
84 this.ws.onmessage = onmessage.bind(this);
86 this.onabort = onabort;
89 function onerror(event) {
96 this.onerror && this.onerror(this);
99 function onopen(event) {
106 function onclose(event) {
107 for (var id in this.pendings) {
108 var ferr = this.pendings[id].onerror;
109 ferr && ferr(null, this);
112 this.onclose && this.onclose();
115 function fire(awaitens, name, data) {
116 var a = awaitens[name];
118 a.forEach(function(handler){handler(data);});
119 var i = name.indexOf("/");
121 a = awaitens[name.substring(0,i)];
123 a.forEach(function(handler){handler(data);});
127 a.forEach(function(handler){handler(data);});
130 function reply(pendings, id, ans, offset) {
131 if (id in pendings) {
132 var p = pendings[id];
139 function onmessage(event) {
140 var obj = JSON.parse(event.data);
144 AFB_context.token = obj[3];
147 reply(this.pendings, id, ans, 0);
150 reply(this.pendings, id, ans, 1);
154 fire(this.awaitens, id, ans);
166 this.onabort = function(){};
169 function call(method, request) {
170 return new Promise((function(resolve, reject){
173 id = String(this.counter = 4095 & (this.counter + 1));
174 } while (id in this.pendings);
175 this.pendings[id] = [ resolve, reject ];
176 arr = [CALL, id, method, request ];
177 if (AFB_context.token) arr.push(AFB_context.token);
178 this.ws.send(JSON.stringify(arr));
182 function onevent(name, handler) {
184 var list = this.awaitens[id] || (this.awaitens[id] = []);
188 AFB_websocket.prototype = {
194 /*********************************************/
198 /*********************************************/
200 context: AFB_context,