forked from socketio/socket.io
-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.js
178 lines (151 loc) · 3.32 KB
/
client.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
/**
* Module dependencies.
*/
var parser = require('socket.io-parser')
, encode = parser.encode
, decode = parser.decode
, debug = require('debug')('socket.io:client');
/**
* Module exports.
*/
module.exports = Client;
/**
* Client constructor.
*
* @param {Server} server instance
* @param {Socket} connection
* @api private
*/
function Client(server, conn){
this.server = server;
this.conn = conn;
this.id = conn.id;
this.request = conn.request;
this.setup();
this.sockets = [];
this.nsps = {};
}
/**
* Sets up event listeners.
*
* @api private
*/
Client.prototype.setup = function(){
this.onclose = this.onclose.bind(this);
this.ondata = this.ondata.bind(this);
this.conn.on('data', this.ondata);
this.conn.on('close', this.onclose);
};
/**
* Connects a client to a namespace.
*
* @param {String} namespace name
* @api private
*/
Client.prototype.connect = function(name){
debug('connecting to namespace %s', name);
var nsp = this.server.of(name);
var self = this;
var socket = nsp.add(this, function(){
self.sockets.push(socket);
self.nsps[nsp.name] = socket;
});
};
/**
* Disconnects from all namespaces and closes transport.
*
* @api private
*/
Client.prototype.disconnect = function(){
var socket;
// we don't use a for loop because the length of
// `sockets` changes upon each iteration
while (socket = this.sockets.shift()) {
socket.disconnect();
}
this.close();
};
/**
* Removes a socket. Called by each `Socket`.
*
* @api private
*/
Client.prototype.remove = function(socket){
var i = this.sockets.indexOf(socket);
if (~i) {
var nsp = this.sockets[i].nsp.name;
this.sockets.splice(i, 1);
delete this.nsps[nsp];
} else {
debug('ignoring remove for %s', socket.id);
}
};
/**
* Closes the underlying connection.
*
* @api private
*/
Client.prototype.close = function(){
if ('open' == this.conn.readyState) {
debug('forcing transport close');
this.conn.close();
this.onclose('forced server close');
}
};
/**
* Writes a packet to the transport.
*
* @param {Object} packet object
* @api private
*/
Client.prototype.packet = function(packet){
if ('open' == this.conn.readyState) {
debug('writing packet %j', packet);
this.conn.write(encode(packet));
} else {
debug('ignoring packet write %j', packet);
}
};
/**
* Called with incoming transport data.
*
* @api private
*/
Client.prototype.ondata = function(data){
var packet = decode(data.toString());
debug('got packet %j', packet);
if (parser.CONNECT == packet.type) {
this.connect(packet.nsp);
} else {
var socket = this.nsps[packet.nsp];
if (socket) {
socket.onpacket(packet);
} else {
debug('no socket for namespace %s', packet.nsp);
}
}
};
/**
* Called upon transport close.
*
* @param {String} reason
* @api private
*/
Client.prototype.onclose = function(reason){
debug('client close with reason %s', reason);
// ignore a potential subsequent `close` event
this.destroy();
// `nsps` and `sockets` are cleaned up seamlessly
this.sockets.forEach(function(socket){
socket.onclose(reason);
});
};
/**
* Cleans up event listeners.
*
* @api private
*/
Client.prototype.destroy = function(){
this.conn.removeListener('data', this.ondata);
this.conn.removeListener('close', this.onclose);
};