-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
sockjs.js
217 lines (202 loc) · 7.22 KB
/
sockjs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
var SockJS = function(url, protocols, options) {
var that = this;
that._options = {devel: false, debug: false, chunking: undefined};
if (options) {
utils.objectExtend(that._options, options);
}
that._base_url = utils.amendUrl(url);
that._server = that._options.server || utils.random_number_string(1000);
that._connid = utils.random_string(8);
that._trans_url = that._base_url + '/' + that._server + '/' + that._connid;
that._protocols = ['websocket',
'xhr-streaming',
'iframe-eventsource',
'iframe-htmlfile',
'xhr-polling',
'iframe-xhr-polling',
'jsonp-polling'];
switch(typeof protocols) {
case 'undefined': break;
case 'string': that._protocols = [protocols]; break;
default: that._protocols = protocols; break;
}
that.protocol = null;
that.readyState = SockJS.CONNECTING;
that._didClose();
};
// Inheritance
SockJS.prototype = new REventTarget();
SockJS.version = "<!-- version -->";
SockJS.CONNECTING = 0;
SockJS.OPEN = 1;
SockJS.CLOSING = 2;
SockJS.CLOSED = 3;
SockJS.prototype._debug = function() {
if (this._options.debug)
utils.log.apply(utils, arguments);
};
SockJS.prototype._dispatchOpen = function() {
var that = this;
if (that.readyState === SockJS.CONNECTING) {
if (that._transport_tref) {
clearTimeout(that._transport_tref);
that._transport_tref = null;
}
that.readyState = SockJS.OPEN;
that.dispatchEvent(new SimpleEvent("open"));
} else {
// The server might have been restarted, and lost track of our
// connection.
that._didClose(1006, "Server lost session");
}
};
SockJS.prototype._dispatchMessage = function(data) {
var that = this;
if (that.readyState !== SockJS.OPEN)
return;
that.dispatchEvent(new SimpleEvent("message", {data: data}));
};
SockJS.prototype._dispatchHeartbeat = function(data) {
var that = this;
if (that.readyState !== SockJS.OPEN)
return;
that.dispatchEvent(new SimpleEvent('heartbeat', {}));
};
SockJS.prototype._didClose = function(code, reason) {
var that = this;
if (that.readyState !== SockJS.CONNECTING &&
that.readyState !== SockJS.OPEN &&
that.readyState !== SockJS.CLOSING)
throw new Error('INVALID_STATE_ERR');
if (that._transport)
that._transport.doCleanup();
that._transport = null;
if (that._transport_tref) {
clearTimeout(that._transport_tref);
that._transport_tref = null;
}
var close_event = new SimpleEvent("close", {code: code,
reason: reason,
wasClean: utils.userSetCode(code)});
if (!utils.userSetCode(code) && that.readyState === SockJS.CONNECTING) {
if (that._try_next_protocol(close_event)) {
that._transport_tref = setTimeout(
function() {
if (that.readyState === SockJS.CONNECTING) {
// I can't understand how it is possible to run
// this timer, when the state is CLOSED, but
// apparently in IE everythin is possible.
that._didClose(2007,
"Transport timeouted");
}
}, 5001);
return;
}
close_event = new SimpleEvent("close", {code: 2000,
reason: "All transports failed",
wasClean: false,
last_event: close_event});
}
that.readyState = SockJS.CLOSED;
utils.delay(function() {
that.dispatchEvent(close_event);
});
};
SockJS.prototype._didMessage = function(data) {
var that = this;
var type = data.slice(0, 1);
switch(type) {
case 'o':
that._dispatchOpen();
break;
case 'a':
var payload = JSON.parse(data.slice(1) || '[]');
for(var i=0; i < payload.length; i++){
that._dispatchMessage(payload[i]);
}
break;
case 'm':
var payload = JSON.parse(data.slice(1) || 'null');
that._dispatchMessage(payload);
break;
case 'c':
var payload = JSON.parse(data.slice(1) || '[]');
that._didClose(payload[0], payload[1]);
break;
case 'h':
that._dispatchHeartbeat();
break;
}
};
SockJS.prototype._try_next_protocol = function(close_event) {
var that = this;
if (that.protocol) {
that._debug('Closed transport:', that.protocol, ''+close_event);
that.protocol = null;
}
while(1) {
var protocol = that.protocol = that._protocols.shift();
if (!protocol) {
return false;
}
// Some protocols require chunking, we may need to run the
// test beforehand.
if (SockJS[protocol] &&
SockJS[protocol].need_chunking === true &&
that._options.chunking === undefined) {
that._protocols.unshift(protocol);
that.protocol = 'chunking-test';
// Assert false, in case test timeouts.
that._options.chunking = false;
chunkingTest(that._base_url, function(chunking) {
that._options.chunking = chunking;
that._try_next_protocol();
}, that._options);
return true;
}
// Some protocols require access to `body`, what if were in
// the `head`?
if (SockJS[protocol] &&
SockJS[protocol].need_body === true &&
!_document.body) {
that._protocols.unshift(protocol);
that.protocol = 'waiting-for-load';
utils.attachEvent('load', function(){
that._try_next_protocol();
});
return true;
}
if (!SockJS[protocol] ||
(SockJS[protocol].need_chunking === true &&
that._options.chunking !== true) ||
!SockJS[protocol].enabled(that._options)) {
that._debug('Skipping transport:', protocol);
} else {
that._debug('Opening transport:', protocol);
that._transport = new SockJS[protocol](that, that._trans_url,
that._base_url);
return true;
}
}
};
SockJS.prototype.close = function(code, reason) {
var that = this;
if (code && !utils.userSetCode(code))
throw new Error("INVALID_ACCESS_ERR");
if(that.readyState !== SockJS.CONNECTING &&
that.readyState !== SockJS.OPEN) {
return false;
}
that.readyState = SockJS.CLOSING;
that._didClose(code || 1000, reason || "Normal closure");
return true;
};
SockJS.prototype.send = function(data) {
var that = this;
if (that.readyState === SockJS.CONNECTING)
throw new Error('INVALID_STATE_ERR');
if (that.readyState === SockJS.OPEN) {
that._transport.doSend(utils.quote('' + data));
}
return true;
};