fix double free error
[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                 ctx = ctx || new AfbCtxItf();
31                 var wl = window.location;
32                 var u = "ws://"+wl.host+"/"+base;
33                 if (ctx.token) {
34                         u = u + '?x-afb-token=' + ctx.token;
35                         if (ctx.uuid)
36                                 u = u + '&x-afb-uuid=' + ctx.uuid;
37                 }
38                 this.ws = new (WebSocket || MozWebSocket)(u, [ "x-afb-ws-json1" ]);
39                 this.pendings = {};
40                 this.counter = 0;
41                 this.ctx = ctx;
42                 this.ws.onopen = onopen.bind(this);
43                 this.ws.onerror = onerror.bind(this);
44                 this.ws.onclose = onclose.bind(this);
45                 this.ws.onmessage = onmessage.bind(this);
46                 this.onopen = onopen;
47                 this.onabort = onabort;
48         }
49
50         function onerror(event) {
51                 var f = this.onabort;
52                 if (f) {
53                         delete this.onopen;
54                         delete this.onabort;
55                         f && f(this);
56                 }
57                 this.onerror && this.onerror(this);
58         }
59
60         function onopen(event) {
61                 var f = this.onopen;
62                 delete this.onopen;
63                 delete this.onabort;
64                 f && f(this);
65         }
66
67         function onclose(event) {
68                 for (var id in this.pendings) {
69                         var ferr = this.pendings[id].onerror;
70                         ferr && ferr(null, this);
71                 }
72                 this.pendings = {};
73                 this.onclose && this.onclose();
74         }
75
76         function onmessage(event) {
77                 var obj = JSON.parse(event.data);
78                 var code = obj[0];
79                 var id = obj[1];
80                 var ans = obj[2];
81                 this.ctx.token = obj[3];
82                 var pend;
83                 if (id && id in this.pendings) {
84                         pend = this.pendings[id];
85                         delete this.pendings[id];
86                 }
87                 switch (code) {
88                 case RETOK:
89                         pend && pend.onsuccess && pend.onsuccess(ans, this);
90                         break; 
91                 case RETERR:
92                 default:
93                         pend && pend.onerror && pend.onerror(ans, this);
94                         break; 
95                 }
96         }
97
98         function close() {
99                 this.ws.close();
100         }
101
102         function call(api, verb, request, onsuccess, onerror) {
103                 var id = String(++this.counter);
104                 this.pendings[id] = { onsuccess: onsuccess, onerror: onerror };
105                 var arr = [CALL, id, api+"/"+verb, request ];
106                 if (this.ctx.token) arr.push(this.ctx.token);
107                 this.ws.send(JSON.stringify(arr));
108         }
109
110         AfbWsItf.prototype = {
111                 close: close,
112                 call: call
113         };
114
115         return AfbWsItf;
116 })();
117