server.js
3.24 KB
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
// this is a test server to support tests which make requests
var io = require('socket.io');
var server = io(process.env.ZUUL_PORT);
var expect = require('expect.js');
server.of('/foo').on('connection', function(){
// register namespace
});
server.of('/timeout_socket').on('connection', function(){
// register namespace
});
server.of('/valid').on('connection', function(){
// register namespace
});
server.of('/asd').on('connection', function(){
// register namespace
});
server.on('connection', function(socket){
// simple test
socket.on('hi', function(){
socket.emit('hi');
});
// ack tests
socket.on('ack', function(){
socket.emit('ack', function(a, b){
if (a == 5 && b.test) {
socket.emit('got it');
}
});
});
socket.on('getAckDate', function(data, cb){
cb(new Date());
});
socket.on('getDate', function(){
socket.emit('takeDate', new Date());
});
socket.on('getDateObj', function(){
socket.emit('takeDateObj', { date: new Date() });
});
socket.on('getUtf8', function() {
socket.emit('takeUtf8', 'てすと');
socket.emit('takeUtf8', 'Я Б Г Д Ж Й');
socket.emit('takeUtf8', 'Ä ä Ü ü ß');
socket.emit('takeUtf8', 'utf8 — string');
socket.emit('takeUtf8', 'utf8 — string');
});
// false test
socket.on('false', function(){
socket.emit('false', false);
});
// binary test
socket.on('doge', function(){
buf = new Buffer('asdfasdf', 'utf8');
socket.emit('doge', buf);
});
// expect receiving binary to be buffer
socket.on('buffa', function(a){
if (Buffer.isBuffer(a)) socket.emit('buffack');
});
// expect receiving binary with mixed JSON
socket.on('jsonbuff', function(a) {
expect(a.hello).to.eql('lol');
expect(Buffer.isBuffer(a.message)).to.be(true);
expect(a.goodbye).to.eql('gotcha');
socket.emit('jsonbuff-ack');
});
// expect receiving buffers in order
var receivedAbuff1 = false;
socket.on('abuff1', function(a) {
expect(Buffer.isBuffer(a)).to.be(true);
receivedAbuff1 = true;
});
socket.on('abuff2', function(a) {
expect(receivedAbuff1).to.be(true);
socket.emit('abuff2-ack');
});
// expect sent blob to be buffer
socket.on('blob', function(a){
if (Buffer.isBuffer(a)) socket.emit('back');
});
// expect sent blob mixed with json to be buffer
socket.on('jsonblob', function(a) {
expect(a.hello).to.eql('lol');
expect(Buffer.isBuffer(a.message)).to.be(true);
expect(a.goodbye).to.eql('gotcha');
socket.emit('jsonblob-ack');
});
// expect blobs sent in order to arrive in correct order
var receivedblob1 = false;
var receivedblob2 = false;
socket.on('blob1', function(a) {
expect(Buffer.isBuffer(a)).to.be(true);
receivedblob1 = true;
});
socket.on('blob2', function(a) {
expect(receivedblob1).to.be(true);
expect(a).to.eql('second');
receivedblob2 = true;
});
socket.on('blob3', function(a) {
expect(Buffer.isBuffer(a)).to.be(true);
expect(receivedblob1).to.be(true);
expect(receivedblob2).to.be(true);
socket.emit('blob3-ack');
});
// emit buffer to base64 receiving browsers
socket.on('getbin', function() {
buf = new Buffer('asdfasdf', 'utf8');
socket.emit('takebin', buf);
});
});