-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathserver.js
250 lines (231 loc) · 5.78 KB
/
server.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
var url = "http://" + (typeof location !== "undefined" ? location.hostname : "localhost") + ":8080/test",
isNode = typeof exports === "object",
isCrossDomain = !new RegExp("^" + portal.support.getAbsoluteURL("/")).test(portal.support.getAbsoluteURL(url)),
text2KB = (function() {
var i, text = "A";
for (i = 0; i < 2048; i++) {
text += "A";
}
return text;
})();
helper.each({
ws: {
can: isNode || typeof WebSocket !== "undefined"
},
sse: {
can: isNode || typeof EventSource !== "undefined"
},
streamxhr: {
can: typeof XMLHttpRequest !== "undefined" &&
(!portal.support.browser.msie || +portal.support.browser.version.split(".")[0] > 9) &&
(!isCrossDomain || portal.support.corsable)
},
streamxdr: {
can: typeof XDomainRequest !== "undefined",
setup: function() {
portal.defaults.xdrURL = function(url) {
return url;
};
},
test: function() {
test("transprot should be skipped if xdrURL is false", 1, function() {
portal.open(url, {xdrURL: false}).close(function(reason) {
strictEqual(reason, "notransport");
});
});
test("transport should be skipped if xdrURL returns false", 1, function() {
portal.open(url, {
xdrURL: function() {
return false;
}
})
.close(function(reason) {
strictEqual(reason, "notransport");
});
});
}
},
streamiframe: {
can: (function() {
try {
// In IE 11 typeof ActiveXObject is undefined,
// but new ActiveXObject("htmlfile") returns something but doesn't work
return ActiveXObject && new ActiveXObject("htmlfile") && !isCrossDomain;
} catch(e) {
return false;
}
})()
},
longpollajax: {
can: isNode || (!isCrossDomain || portal.support.corsable)
},
longpollxdr: {
can: typeof XDomainRequest !== "undefined",
setup: function() {
portal.defaults.xdrURL = function(url) {
return url;
};
},
test: function() {
test("transprot should be skipped if xdrURL is false", 1, function() {
portal.open(url, {xdrURL: false}).close(function(reason) {
strictEqual(reason, "notransport");
});
});
test("transport should be skipped if xdrURL returns false", 1, function() {
portal.open(url, {
xdrURL: function() {
return false;
}
})
.close(function(reason) {
strictEqual(reason, "notransport");
});
});
}
},
longpolljsonp: {
can: !isNode
}
}, function(transport, group) {
QUnit.module(transport, {
setup: function() {
helper.setup();
portal.defaults.transports = [transport];
portal.defaults.notifyAbort = true;
if (group.setup) {
group.setup();
}
},
teardown: function() {
helper.teardown();
if (group.teardown) {
group.teardown();
}
}
});
if (group.can) {
transportTest();
if (group.test) {
group.test();
}
} else {
test(transport + " is not supported by the browser", helper.okTrue);
}
});
function transportTest() {
asyncTest("transport should estabish a connection on socket.open()", 1, function() {
portal.open(url).open(function() {
ok(true);
start();
});
});
asyncTest("transport should be able to exchange an event", 1, function() {
portal.open(url).on("echo", function(data) {
strictEqual(data, "data");
start();
})
.send("echo", "data");
});
asyncTest("transport should be able to exchange one hundred of event with the interval of 1ms", 1, function() {
var i, array = [], returned = [];
for (i = 0; i < 100; i++) {
array.push(i + 1);
}
portal.open(url).on({
open: function() {
var self = this;
for (i = 0; i < array.length; i++) {
(function(elem) {
setTimeout(function() {
self.send("echo", elem);
}, 1);
})(array[i]);
}
},
echo: function(data) {
returned.push(data);
if (returned.length === array.length) {
returned.sort(function(a, b) {
return a - b;
});
deepEqual(returned, array);
start();
}
}
});
});
asyncTest("trnasport should be able to exchange an event consisting of multi-byte characters", 1, function() {
portal.open(url).on("echo", function(data) {
strictEqual(data, "안녕");
start();
})
.send("echo", "안녕");
});
asyncTest("transport should be able to exchange an event of 2KB", 1, function() {
portal.open(url).on("echo", function(data) {
strictEqual(data, text2KB);
start();
})
.send("echo", text2KB);
});
asyncTest("transport should close connection on socket.close()", 1, function() {
portal.open(url).open(function() {
ok(true);
this.close();
})
.close(function() {
start();
});
});
asyncTest("transport should be notified of close", 1, function() {
portal.open(url).open(function() {
this.send("disconnect");
ok(true);
})
.close(function() {
start();
});
});
// From now on they are fully tested in client.js and have nothing to do with
// transport itself, but are provided to help to implement the portal server
asyncTest("transport should support reply by server", 2, function() {
var ready;
portal.open(url).send("reply-by-server", true, function(echo) {
strictEqual(echo, true);
if (ready) {
start();
} else {
ready = true;
}
}, helper.okFalse)
.send("reply-by-server", false, helper.okFalse, function(echo) {
strictEqual(echo, false);
if (ready) {
start();
} else {
ready = true;
}
});
});
asyncTest("transport should support reply by client", 1, function() {
portal.open(url).on("reply-by-client", function(data, reply) {
ok(true);
reply("echo");
})
.on("echo", function() {
start();
})
.send("reply-by-client");
});
asyncTest("transport should support heartbeat", 5, function() {
var i = 0;
portal.open(url, {heartbeat: 1500, _heartbeat: 500}).on("heartbeat", function() {
i++;
ok(true);
if (i > 4) {
start();
}
});
});
}