Mercurial > eldonilo > lightstring
annotate transports/bosh.js @ 105:fb50311997b5
Add EventEmitter.js lib
author | Sonny Piers <sonny.piers@gmail.com> |
---|---|
date | Wed, 13 Jun 2012 12:48:06 +0200 |
parents | 3e124209821a |
children | c06ec02217ee |
rev | line source |
---|---|
100
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
1 'use strict'; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
2 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
3 (function() { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
4 Lightstring.BOSHConnection = function(aService) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
5 this.service = aService; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
6 this.rid = 1337; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
7 this.currentRequests = 0; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
8 this.maxHTTPRetries = 5; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
9 this.maxRequests = 2; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
10 this.queue = []; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
11 }; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
12 Lightstring.BOSHConnection.prototype = new EventEmitter(); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
13 Lightstring.BOSHConnection.prototype.open = function() { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
14 var that = this; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
15 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
16 var attrs = { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
17 wait: '60', |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
18 hold: '1', |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
19 to: 'yuilop', |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
20 content: 'text/xml; charset=utf-8', |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
21 ver: '1.6', |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
22 'xmpp:version': '1.0', |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
23 'xmlns:xmpp': 'urn:xmpp:xbosh', |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
24 }; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
25 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
26 this.request(attrs, null, function(data) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
27 that.emit('open'); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
28 that.sid = data.getAttribute('sid'); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
29 that.maxRequests = data.getAttribute('maxRequests') || that.maxRequests; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
30 }); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
31 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
32 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
33 this.on('in', function(stanza) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
34 if (stanza.localName === 'success') { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
35 that.request({ |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
36 'xmpp:restart': 'true', |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
37 'xmlns:xmpp': 'urn:xmpp:xbosh' |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
38 }) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
39 } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
40 }) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
41 }; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
42 Lightstring.BOSHConnection.prototype.request = function(attrs, children, aOnSuccess, aOnError, aRetry) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
43 // if (children && children[0] && children[0].name === 'body') { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
44 // var body = children[0]; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
45 // } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
46 // else { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
47 // var body = new ltx.Element('body'); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
48 // if (children) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
49 // if(util.isArray(children)) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
50 // for (var k in children) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
51 // body.cnode(children[k]); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
52 // else |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
53 // body.cnode(children); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
54 // } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
55 // } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
56 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
57 var body = '<body rid="' + this.rid++ + '" xmlns="http://jabber.org/protocol/httpbind"/>'; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
58 var body = Lightstring.XML2DOM(body); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
59 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
60 //sid |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
61 if (this.sid) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
62 body.setAttribute('sid', this.sid); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
63 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
64 //attributes on body |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
65 for (var i in attrs) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
66 body.setAttribute(i, attrs[i]); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
67 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
68 //children |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
69 for (var i in children) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
70 body.appendChild(children[i]); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
71 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
72 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
73 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
74 var retry = aRetry || 0; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
75 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
76 var req = new XMLHttpRequest(); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
77 req.open('POST', this.service); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
78 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
79 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
80 // req.upload.addEventListener("progress", updateProgress, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
81 // req.upload.addEventListener("load", transferComplete, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
82 // req.upload.addEventListener("error", transferFailed, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
83 // req.upload.addEventListener("abort", transferCanceled, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
84 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
85 // req.addEventListener("progress", updateProgress, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
86 // req.addEventListener("load", transferComplete, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
87 // req.addEventListener("error", transferFailed, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
88 // req.addEventListener("abort", transferCanceled, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
89 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
90 var that = this; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
91 // req.responseType = 'document'; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
92 req.addEventListener("load", function() { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
93 if (req.status < 200 || req.status >= 400) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
94 that.emit('error', "HTTP status " + req.status); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
95 that.emit('close'); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
96 return; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
97 } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
98 that.currentRequests--; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
99 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
100 var body = this.response; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
101 that.emit('rawin', body); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
102 var bodyEl = Lightstring.XML2DOM(body); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
103 that.processResponse(bodyEl) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
104 if (aOnSuccess) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
105 aOnSuccess(bodyEl); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
106 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
107 }, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
108 // req.on('error', function(error) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
109 // if (retry < that.maxHTTPRetries) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
110 // that.request(attrs, children, aOnSuccess, aOnError, ++retry); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
111 // } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
112 // else { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
113 // that.emit('close'); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
114 // that.emit('error', error); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
115 // if (aOnError) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
116 // aOnError(error); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
117 // } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
118 // }); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
119 // this.emit('rawout', body.toString()); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
120 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
121 for(var i = 0; i < body.children.length; i++) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
122 var child = body.children[i]; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
123 that.emit('out', child); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
124 } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
125 this.emit('rawout', Lightstring.DOM2XML(body)) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
126 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
127 req.send(Lightstring.DOM2XML(body)); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
128 this.currentRequests++; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
129 }; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
130 Lightstring.BOSHConnection.prototype.send = function(aData) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
131 if (!aData) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
132 var el = ''; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
133 } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
134 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
135 else if(typeof aData == 'string') { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
136 try { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
137 var el = Lightstring.XML2DOM(aData); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
138 } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
139 catch(e) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
140 console.log(e); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
141 console.log(aData); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
142 } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
143 } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
144 else { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
145 var el = aData.root(); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
146 } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
147 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
148 var that = this; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
149 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
150 this.queue.push(el); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
151 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
152 setTimeout(this.mayRequest.bind(this), 0) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
153 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
154 }; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
155 Lightstring.BOSHConnection.prototype.end = function(stanzas) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
156 var that = this; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
157 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
158 stanzas = stanzas || []; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
159 if (typeof stanzas !== 'array') |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
160 stanzas = [stanzas]; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
161 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
162 stanzas = this.queue.concat(stanzas); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
163 this.queue = []; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
164 this.request({type: 'terminate'}, stanzas, |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
165 function(err, bodyEl) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
166 that.emit('end'); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
167 that.emit('close'); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
168 delete that.sid; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
169 }); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
170 }; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
171 Lightstring.BOSHConnection.prototype.processResponse = function(bodyEl) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
172 if (bodyEl && bodyEl.children) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
173 for(var i = 0; i < bodyEl.children.length; i++) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
174 var child = bodyEl.children[i]; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
175 this.emit('in', child); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
176 } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
177 } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
178 if (bodyEl && bodyEl.getAttribute('type') === 'terminate') { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
179 var condition = bodyEl.getAttribute('condition'); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
180 this.emit('error', |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
181 new Error(condition || "Session terminated")); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
182 this.emit('close'); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
183 } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
184 }; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
185 Lightstring.BOSHConnection.prototype.mayRequest = function() { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
186 var canRequest = |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
187 this.sid && (this.currentRequests === 0 || ((this.queue.length > 0 && this.currentRequests < this.maxRequests)) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
188 ); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
189 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
190 if (!canRequest) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
191 return; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
192 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
193 var stanzas = this.queue; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
194 this.queue = []; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
195 //~ this.rid++; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
196 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
197 var that = this; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
198 this.request({}, stanzas, |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
199 //success |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
200 function(data) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
201 //if (data) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
202 //that.processResponse(data); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
203 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
204 setTimeout(that.mayRequest.bind(that), 0); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
205 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
206 }, |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
207 //error |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
208 function(error) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
209 that.emit('error', error); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
210 that.emit('close'); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
211 delete that.sid; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
212 } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
213 ); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
214 }; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
215 })(); |