summaryrefslogtreecommitdiffstats
path: root/htdocs
diff options
context:
space:
mode:
Diffstat (limited to 'htdocs')
-rw-r--r--htdocs/AFB.js (renamed from htdocs/AFB-websock.js)85
-rw-r--r--htdocs/index.html4
2 files changed, 64 insertions, 25 deletions
diff --git a/htdocs/AFB-websock.js b/htdocs/AFB.js
index 3d4831f..b07efc5 100644
--- a/htdocs/AFB-websock.js
+++ b/htdocs/AFB.js
@@ -1,10 +1,32 @@
-var urlWS;
-var urlhttp;
-
+/*
+ * Copyright (C) 2017, 2018 "IoT.bzh"
+ * Author: José Bollo <jose.bollo@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
AFB = function(base, initialtoken){
-urlWS = "ws://"+window.location.host+"/"+base;
-urlhttp = "http://"+window.location.host+"/"+base;
+if (typeof base != "object")
+ base = { base: base, token: initialtoken };
+
+var initial = {
+ base: base.base || "api",
+ token: base.token || initialtoken || "HELLO",
+ host: base.host || window.location.host,
+ url: base.url || undefined
+};
+
+var urlws = initial.url || "ws://"+initial.host+"/"+initial.base;
/*********************************************/
/**** ****/
@@ -14,7 +36,7 @@ urlhttp = "http://"+window.location.host+"/"+base;
var AFB_context;
{
var UUID = undefined;
- var TOKEN = initialtoken;
+ var TOKEN = initial.token;
var context = function(token, uuid) {
this.token = token;
@@ -44,14 +66,16 @@ var AFB_websocket;
var PROTO1 = "x-afb-ws-json1";
- AFB_websocket = function(onopen, onabort) {
- var u = urlWS;
+ AFB_websocket = function(on_open, on_abort) {
+ var u = urlws, p = '?';
if (AFB_context.token) {
u = u + '?x-afb-token=' + AFB_context.token;
- if (AFB_context.uuid)
- u = u + '&x-afb-uuid=' + AFB_context.uuid;
+ p = '&';
}
+ if (AFB_context.uuid)
+ u = u + p + 'x-afb-uuid=' + AFB_context.uuid;
this.ws = new WebSocket(u, [ PROTO1 ]);
+ this.url = u;
this.pendings = {};
this.awaitens = {};
this.counter = 0;
@@ -59,9 +83,8 @@ var AFB_websocket;
this.ws.onerror = onerror.bind(this);
this.ws.onclose = onclose.bind(this);
this.ws.onmessage = onmessage.bind(this);
- this.onopen = onopen;
- this.onabort = onabort;
- this.onclose = onabort;
+ this.onopen = on_open;
+ this.onabort = on_abort;
}
function onerror(event) {
@@ -69,7 +92,7 @@ var AFB_websocket;
if (f) {
delete this.onopen;
delete this.onabort;
- f && f(this);
+ f(this);
}
this.onerror && this.onerror(this);
}
@@ -82,9 +105,15 @@ var AFB_websocket;
}
function onclose(event) {
+ var err = {
+ jtype: 'afb-reply',
+ request: {
+ status: 'disconnected',
+ info: 'server hung up'
+ }
+ };
for (var id in this.pendings) {
- var ferr = this.pendings[id].onerror;
- ferr && ferr(null, this);
+ try { this.pendings[id][1](err); } catch (x) {/*NOTHING*/}
}
this.pendings = {};
this.onclose && this.onclose();
@@ -109,8 +138,7 @@ var AFB_websocket;
if (id in pendings) {
var p = pendings[id];
delete pendings[id];
- var f = p[offset];
- f(ans);
+ try { p[offset](ans); } catch (x) {/*TODO?*/}
}
}
@@ -136,15 +164,26 @@ var AFB_websocket;
function close() {
this.ws.close();
- this.onabort();
+ this.ws.onopen =
+ this.ws.onerror =
+ this.ws.onclose =
+ this.ws.onmessage =
+ this.onopen =
+ this.onabort = function(){};
}
- function call(method, request) {
+ function call(method, request, callid) {
return new Promise((function(resolve, reject){
var id, arr;
- do {
- id = String(this.counter = 4095 & (this.counter + 1));
- } while (id in this.pendings);
+ if (callid) {
+ id = String(callid);
+ if (id in this.pendings)
+ throw new Error("pending callid("+id+") exists");
+ } else {
+ do {
+ id = String(this.counter = 4095 & (this.counter + 1));
+ } while (id in this.pendings);
+ }
this.pendings[id] = [ resolve, reject ];
arr = [CALL, id, method, request ];
if (AFB_context.token) arr.push(AFB_context.token);
diff --git a/htdocs/index.html b/htdocs/index.html
index 5a84ee8..0480c35 100644
--- a/htdocs/index.html
+++ b/htdocs/index.html
@@ -3,7 +3,7 @@
<head>
<title>VSHL API Test</title>
<link rel="stylesheet" href="binding.css">
- <script type="text/javascript" src="AFB-websock.js"></script>
+ <script type="text/javascript" src="AFB.js"></script>
<script type="text/javascript" src="binding.js"></script>
</head>
@@ -92,4 +92,4 @@
</div>
</div>
-</body> \ No newline at end of file
+</body>