new websocket handling
[src/app-framework-binder.git] / test / websock.js
1
2 AfbCtxItf = (function(){
3
4         var UUID = undefined;
5         var TOKEN = undefined;
6
7         function AfbCtxItf(token, uuid) {
8                 this.token = token;
9                 this.uuid = uuid;
10         }
11
12         AfbCtxItf.prototype = {
13                 get token() {return TOKEN;},
14                 set token(tok) {if(tok) TOKEN=tok;},
15                 get uuid() {return UUID;},
16                 set uuid(id) {if(id) UUID=id;}
17         };
18
19         return AfbCtxItf;
20 })();
21
22
23 AfbWsItf = (function(){
24
25         var CALL = 2;
26         var RETOK = 3;
27         var RETERR = 4;
28
29         function AfbWsItf(base, onopen, onabort, ctx) {
30                 var wl = window.location;
31                 var u = "ws://"+wl.host+"/"+base;
32                 this.ws = new (WebSocket || MozWebSocket)(u, [ "x-afb-ws-json1" ]);
33                 this.pendings = {};
34                 this.counter = 0;
35                 this.ctx = ctx || new AfbCtxItf();
36                 this.ws.onopen = onopen.bind(this);
37                 this.ws.onerror = onerror.bind(this);
38                 this.ws.onclose = onclose.bind(this);
39                 this.ws.onmessage = onmessage.bind(this);
40                 this.onopen = onopen;
41                 this.onabort = onabort;
42         }
43
44         function onerror(event) {
45                 var f = this.onabort;
46                 if (f) {
47                         delete this.onopen;
48                         delete this.onabort;
49                         f && f(this);
50                 }
51                 this.onerror && this.onerror(this);
52         }
53
54         function onopen(event) {
55                 var f = this.onopen;
56                 delete this.onopen;
57                 delete this.onabort;
58                 f && f(this);
59         }
60
61         function onclose(event) {
62                 for (var id in this.pendings) {
63                         var ferr = this.pendings[id].onerror;
64                         ferr && ferr(null, this);
65                 }
66                 this.pendings = {};
67                 this.onclose && this.onclose();
68         }
69
70         function onmessage(event) {
71                 var obj = JSON.parse(event.data);
72                 var code = obj[0];
73                 var id = obj[1];
74                 var ans = obj[2];
75                 this.ctx.token = obj[3];
76                 var pend;
77                 if (id && id in this.pendings) {
78                         pend = this.pendings[id];
79                         delete this.pendings[id];
80                 }
81                 switch (code) {
82                 case RETOK:
83                         pend && pend.onsuccess && pend.onsuccess(ans, this);
84                         break; 
85                 case RETERR:
86                 default:
87                         pend && pend.onerror && pend.onerror(ans, this);
88                         break; 
89                 }
90         }
91
92         function close() {
93                 this.ws.close();
94         }
95
96         function call(api, verb, request, onsuccess, onerror) {
97                 var id = String(++this.counter);
98                 this.pendings[id] = { onsuccess: onsuccess, onerror: onerror };
99                 var arr = [CALL, id, api+"/"+verb, request ];
100                 if (this.ctx.token) arr.push(this.ctx.token);
101                 this.ws.send(JSON.stringify(arr));
102         }
103
104         AfbWsItf.prototype = {
105                 close: close,
106                 call: call
107         };
108
109         return AfbWsItf;
110 })();
111