1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | (function (exports, io) {
|
9 |
|
10 | |
11 |
|
12 |
|
13 |
|
14 | exports.Transport = Transport;
|
15 |
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function Transport (socket, sessid) {
|
24 | this.socket = socket;
|
25 | this.sessid = sessid;
|
26 | };
|
27 |
|
28 | |
29 |
|
30 |
|
31 |
|
32 | io.util.mixin(Transport, io.EventEmitter);
|
33 |
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | Transport.prototype.onData = function (data) {
|
44 | this.clearCloseTimeout();
|
45 | this.setCloseTimeout();
|
46 |
|
47 | if (data !== '') {
|
48 |
|
49 | var msgs = io.parser.decodePayload(data);
|
50 |
|
51 | if (msgs && msgs.length) {
|
52 | for (var i = 0, l = msgs.length; i < l; i++) {
|
53 | this.onPacket(msgs[i]);
|
54 | }
|
55 | }
|
56 | }
|
57 |
|
58 | return this;
|
59 | };
|
60 |
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | Transport.prototype.onPacket = function (packet) {
|
68 | if (packet.type == 'heartbeat') {
|
69 | return this.onHeartbeat();
|
70 | }
|
71 |
|
72 | if (packet.type == 'connect' && packet.endpoint == '') {
|
73 | this.onConnect();
|
74 | }
|
75 |
|
76 | this.socket.onPacket(packet);
|
77 |
|
78 | return this;
|
79 | };
|
80 |
|
81 | |
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | Transport.prototype.setCloseTimeout = function () {
|
88 | if (!this.closeTimeout) {
|
89 | var self = this;
|
90 |
|
91 | this.closeTimeout = setTimeout(function () {
|
92 | self.onDisconnect();
|
93 | }, this.socket.closeTimeout);
|
94 | }
|
95 | };
|
96 |
|
97 | |
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | Transport.prototype.onDisconnect = function () {
|
104 | if (this.close) this.close();
|
105 | this.clearTimeouts();
|
106 | this.socket.onDisconnect();
|
107 | return this;
|
108 | };
|
109 |
|
110 | |
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | Transport.prototype.onConnect = function () {
|
117 | this.socket.onConnect();
|
118 | return this;
|
119 | }
|
120 |
|
121 | |
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | Transport.prototype.clearCloseTimeout = function () {
|
128 | if (this.closeTimeout) {
|
129 | clearTimeout(this.closeTimeout);
|
130 | this.closeTimeout = null;
|
131 | }
|
132 | };
|
133 |
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | Transport.prototype.clearTimeouts = function () {
|
141 | this.clearCloseTimeout();
|
142 |
|
143 | if (this.reopenTimeout) {
|
144 | clearTimeout(this.reopenTimeout);
|
145 | }
|
146 | };
|
147 |
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | Transport.prototype.packet = function (packet) {
|
156 | this.send(io.parser.encodePacket(packet));
|
157 | };
|
158 |
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | Transport.prototype.onHeartbeat = function (heartbeat) {
|
168 | this.packet({ type: 'heartbeat' });
|
169 | };
|
170 |
|
171 | |
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | Transport.prototype.onOpen = function () {
|
178 | this.open = true;
|
179 | this.clearCloseTimeout();
|
180 | this.socket.onOpen();
|
181 | };
|
182 |
|
183 | |
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | Transport.prototype.onClose = function () {
|
191 | var self = this;
|
192 |
|
193 | |
194 |
|
195 |
|
196 |
|
197 |
|
198 | this.open = false;
|
199 | this.setCloseTimeout();
|
200 | this.socket.onClose();
|
201 | };
|
202 |
|
203 | |
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | Transport.prototype.prepareUrl = function () {
|
212 | var options = this.socket.options;
|
213 |
|
214 | return this.scheme() + '://'
|
215 | + options.host + ':' + options.port + '/'
|
216 | + options.resource + '/' + io.protocol
|
217 | + '/' + this.name + '/' + this.sessid;
|
218 | };
|
219 |
|
220 | |
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 | Transport.prototype.ready = function (socket, fn) {
|
229 | fn.call(this);
|
230 | };
|
231 | })(
|
232 | 'undefined' != typeof io ? io : module.exports
|
233 | , 'undefined' != typeof io ? io : module.parent.exports
|
234 | );
|