forked from mvayngrib/socketgd
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.js
More file actions
125 lines (113 loc) · 3.44 KB
/
Copy pathtest.js
File metadata and controls
125 lines (113 loc) · 3.44 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
var should = require('should');
var http = require('http');
var ios = require('socket.io');
var ioc = require('socket.io-client');
var SocketGD = require('./socketgd').SocketGD;
var port = 10101;
var app;
var ioserver;
function client(of, cb) {
var socketgd = new SocketGD(ioc('http://localhost:'+port+of));
socketgd.on('connect', function() {
cb && cb(socketgd);
cb = null;
});
return socketgd;
}
describe('Socket GD', function() {
before(function(done) {
app = http.createServer();
ioserver = ios(app);
app.listen(port, done);
});
beforeEach(function(done) {
done();
});
afterEach(function(done) {
done();
});
it('sends and receives messages normally', function(done) {
var events = 0;
var ssgd = new SocketGD();
ioserver.of('/1').on('connection', function(socket) {
ssgd.setSocket(socket);
ssgd.on('message', function(message) {
++events;
message.should.be.exactly('hello server');
});
ssgd.on('event1', function(event) {
++events;
event.hello.should.be.exactly('world');
event.number.should.be.exactly(1);
event.boolean.should.be.exactly(true);
ssgd.send('hello client');
ssgd.emit('event1', {hello: 'world', number: 1, boolean: true});
ssgd.emit('event2', {hello2: 'world2'});
});
ssgd.on('event2', function(event) {
++events;
event.hello2.should.be.exactly('world2');
});
});
var csgd = new SocketGD(ioc('http://localhost:'+port+'/1', {transports: ['websocket'], forceNew: true}));
csgd.on('connect', function() {
csgd.send('hello server');
csgd.emit('event1', {hello: 'world', number: 1, boolean: true});
csgd.emit('event2', {hello2: 'world2'});
});
csgd.on('message', function(message) {
++events;
message.should.be.exactly('hello client');
});
csgd.on('event1', function(event) {
++events;
event.hello.should.be.exactly('world');
event.number.should.be.exactly(1);
event.boolean.should.be.exactly(true);
});
csgd.on('event2', function(event) {
++events;
event.hello2.should.be.exactly('world2');
events.should.be.exactly(6);
done();
});
});
it('sends and receives messages after disconnect', function(done) {
var sevents = 0;
var cevents = 0;
var ssgd = new SocketGD();
ioserver.of('/2').on('connection', function(socket) {
ssgd.setSocket(socket);
ssgd.on('message', function(message, ack) {
++sevents;
if (sevents === 2) {
// terminate the socket after the second message
// and don't ack it so it should be received again when the connection is up again
ssgd.disconnect(true);
return;
}
ssgd.send(message);
ack();
});
});
var csgd = new SocketGD(ioc('http://localhost:'+port+'/2', {transports: ['websocket'], forceNew: true}));
var sendMessages = function() {
csgd.send('hello server 1');
csgd.send('hello server 2');
csgd.send('hello server 3');
sendMessages = null;
};
csgd.on('connect', function() {
sendMessages && sendMessages();
});
csgd.on('message', function(message, ack, msgId) {
++cevents;
ack();
if (msgId === 2) {
sevents.should.be.exactly(4); // the server should receive the second message twice
cevents.should.be.exactly(3);
done();
}
});
});
});