initial commit
[namibia] / public / js / vendor / ape-source / Transport / Transport.WebSocket.js
1 APE.Transport.WebSocket = new Class({
2
3         stack: [],
4         connRunning: false,
5
6         initialize: function(ape) {
7                 this.ape = ape;
8                 this.initWs();
9         },
10
11         initWs: function() {
12                 this.ws = new WebSocket( (this.ape.options.secure ? 'wss' : 'ws') + '://' + this.ape.options.frequency + '.' + this.ape.options.server + '/' + this.ape.options.transport +'/');
13                 this.connRunning = true;
14                 this.ws.onmessage = this.readWs.bind(this);
15                 this.ws.onopen = this.openWs.bind(this);
16                 this.ws.onclose = this.closeWs.bind(this);
17                 this.ws.onerror = this.errorWs.bind(this);
18         },
19
20         readWs: function(evt) {
21                 this.ape.parseResponse(evt.data, this.callback);
22                 this.callback = null;
23         },
24
25         openWs: function() {
26                 if (this.stack.length > 0) {
27                         for (var i = 0; i < this.stack.length; i++) this.send(this.stack[i].q, this.stack[i].options);
28                         this.stack.length = 0;
29                 }
30         },
31
32         closeWs: function() {
33                 this.connRunning = false;
34         },
35
36         errorWs: function() {
37                 this.connRunning = false;
38         },
39
40         send: function(queryString, options) {
41                 if (this.ws.readyState == 1) {
42                         if (options.requestCallback) this.callback = options.requestCallback;
43                         this.ws.send(queryString);
44                 } else {//ws not connect, stack request
45                         this.stack.push({'q': queryString, 'options': options});
46                 }
47         },
48
49         running: function() {
50                 return this.connRunning;
51         },
52
53         cancel: function() {
54                 this.ws.close();
55         }
56
57 });
58
59 APE.Transport.WebSocket.browserSupport = function() {
60         if ('WebSocket' in window) return true;
61         else return 1;//No websocket support switch to XHRStreaming
62 }