File size: 4,883 Bytes
5fae594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
'use strict';

/*
 * Dependencies
 */

var EventEmitter = require('events').EventEmitter;
var Namespace    = require('./namespace');
var Callbacks    = require('./callbacks');
var Room         = require('./room');
var inherits     = require('./util').inherits;
var Broadcast    = require('./broadcast').init(Room);
var handle       = require('./handle');
var Message      = require('./message');

/*
 * Socket Constructor
 *
 * - [socket] (object) : socket to send and receive messages over
 * - [handle] (string|object) : socket interface
 */

var Socket = function Socket (socket, handle) {
  Socket.__super__.call(this);

  // public properties
  this.rooms = [];
  this.broadcast = Broadcast.bind(this);

  // private properties
  this._namespaces = {};
  this._callbacks = new Callbacks(this.namespace('socket'));
  this._message = new Message(this._callbacks);

  this.join('all');

  // Set up socket and handle
  if (socket) this.connect(socket, handle);

};


/*
 * Inherit from EventEmitter
 */

inherits(Socket, EventEmitter);
Socket.prototype._emit = EventEmitter.prototype.emit;


/*
 * (Static) All
 */

Socket.all = Room.get('all');


/*
 * (Static) In
 */

Socket.in = Room.get;


/*
 * (Static) Rooms
 */

Socket.rooms = Room.rooms;


/*
 * (Private) Process
 * Processes messages received on the socket
 *
 * - data (string)
 */

Socket.prototype._process = function _process (data) {
  var message = this._message.parse(data);
  var details = Namespace.parse(message.event);

  var arg1 = message.arg1;
  var arg2 = message.arg2;
  var arg3 = message.arg3;

  var event = details.event;
  var namespace = details.namespace;
  var ns = this._namespaces[namespace];

  if (namespace && ns) {
    ns._emit(event, arg1, arg2, arg3);
  }

  this._emit(message.event, arg1, arg2, arg3);
};



/*
 * (Private) Handle With
 *
 * - name (string|object) : the name of a handler, or a handler
 */

Socket.prototype._handleWith = function _handleWith (name) {
  this._handle = handle(name);
};


/*
 * Connect
 * Connect to a socket
 *
 * - socket (Object)
 */

Socket.prototype.connect = function connect (socket, handle) {
  var self = this;

  if (handle) this._handleWith(handle);

  this.socket = socket;
  this.id = this._handle.identify(socket);

  this._handle.onread(this.socket, function handleOnRead (message) {
    self._process(message);
  });

  this._handle.onopen(this.socket, function handleOnOpen (event) {
    self._emit('socket.open', event);
  });

  this._handle.onerror(this.socket, function handleOnError (event) {
    self._emit('socket.error', event);
  });

  this._handle.onclose(this.socket, function handleOnClose (status, message) {
    self.disconnect();
    self._emit('socket.close', status, message);
  });
};


/*
 * Disconnnect
 * The opposite of `connect`.
 *
 * Remove the socket from any rooms it is in.
 * Also release control of the socket.
 */

Socket.prototype.disconnect = function disconnect () {
  this._handle.release(this.socket);

  var len = this.rooms.length;
  for (var i = len - 1; i >= 0; i--) {
    this.leave(this.rooms[i]);
  }
};


/*
 * Namespace
 * If the namespace already exists it will be used instead
 *
 * - name (string)
 * > namespace
 */

Socket.prototype.namespace = function namespace (name) {
  var ns = this._namespaces[name];
  ns = ns ? ns : this._namespaces[name] = new Namespace(name, this);
  return ns;
};


/*
 * Send a message through the socket
 *
 * - event (string)
 * - args... (mixed) : any data you want to send
 */

Socket.prototype.emit = function emit (event, arg1, arg2, arg3) {
  if (event === 'newListener' || event === 'removeListener') {
    return this._emit(event, arg1, arg2, arg3);
  }

  var message = this._message.serialize(event, arg1, arg2, arg3);

  this._handle.write(this.socket, message);
};




/*
 * Join
 *
 * - room (string)
 */

Socket.prototype.join = function join (room) {
  var index = this.rooms.indexOf(room);
  if (index < 0) {
    this.rooms.push(room);
    room = Room.get(room);
    room._join(this);
  }
};


/*
 * Leave
 *
 * - room (string)
 */

Socket.prototype.leave = function leave (room) {
  var index = this.rooms.indexOf(room);
  if (index > -1) {
    this.rooms.splice(index, 1);
    room = Room.get(room);
    room._leave(this);
  }
};


/*
 * Room
 *
 * - name (string)
 */

Socket.prototype.room = Room.get;


/*
 * Release
 *
 * Forget everything.
 * TODO: Possibly overkill.
 */

Socket.prototype.release = function release () {
  if (! this._message) return;

  this.disconnect();

  for (var key in this._namespaces) {
    this._namespaces[key]._release();
  }

  this._callbacks.release();
  this._message.release();

  delete this._namespaces;
  delete this._callbacks;
  delete this._message;
  delete this.socket;
  delete this.broadcast;
  delete this.rooms;
  delete this.id;
  delete this._handle;
};


module.exports = Socket;