Mercurial > eldonilo > lightstring
annotate transports/bosh.js @ 107:704ce44c1a22
Add several properties to the Lightstring.Stanza object
author | Sonny Piers <sonny@fastmail.net> |
---|---|
date | Thu, 28 Jun 2012 12:51:04 +0200 |
parents | c06ec02217ee |
children | 5cb4733c5189 |
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 |
106 | 57 var body = new Lightstring.Stanza('<body rid="' + this.rid++ + '" xmlns="http://jabber.org/protocol/httpbind"/>'); |
100
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
58 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
59 //sid |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
60 if (this.sid) |
106 | 61 body.el.setAttribute('sid', this.sid); |
100
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
62 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
63 //attributes on body |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
64 for (var i in attrs) |
106 | 65 body.el.setAttribute(i, attrs[i]); |
100
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
66 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
67 //children |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
68 for (var i in children) |
106 | 69 body.el.appendChild(children[i]); |
100
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
70 |
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 var retry = aRetry || 0; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
74 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
75 var req = new XMLHttpRequest(); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
76 req.open('POST', this.service); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
77 |
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 // req.upload.addEventListener("progress", updateProgress, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
80 // req.upload.addEventListener("load", transferComplete, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
81 // req.upload.addEventListener("error", transferFailed, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
82 // req.upload.addEventListener("abort", transferCanceled, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
83 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
84 // req.addEventListener("progress", updateProgress, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
85 // req.addEventListener("load", transferComplete, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
86 // req.addEventListener("error", transferFailed, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
87 // req.addEventListener("abort", transferCanceled, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
88 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
89 var that = this; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
90 // req.responseType = 'document'; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
91 req.addEventListener("load", function() { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
92 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
|
93 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
|
94 that.emit('close'); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
95 return; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
96 } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
97 that.currentRequests--; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
98 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
99 var body = this.response; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
100 that.emit('rawin', body); |
106 | 101 var bodyEl = Lightstring.parse(body); |
100
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
102 that.processResponse(bodyEl) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
103 if (aOnSuccess) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
104 aOnSuccess(bodyEl); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
105 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
106 }, false); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
107 // req.on('error', function(error) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
108 // if (retry < that.maxHTTPRetries) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
109 // 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
|
110 // } |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
111 // else { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
112 // that.emit('close'); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
113 // that.emit('error', error); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
114 // if (aOnError) |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
115 // aOnError(error); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
116 // } |
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 // this.emit('rawout', body.toString()); |
106 | 119 if (body.children) { |
120 for(var i = 0; i < body.children.length; i++) { | |
121 var child = body.children[i]; | |
122 that.emit('out', child); | |
123 } | |
124 } | |
125 this.emit('rawout', body); | |
100
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
126 |
106 | 127 req.send(Lightstring.serialize(body)); |
100
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 { |
106 | 137 var el = Lightstring.parse(aData); |
100
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 })(); |