File size: 989 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
'use strict';


var Broadcast = require('../source/broadcast');
var Room = require('../source/room');
var Namespace = require('../source/namespace');

var lastMessage = {};
var emitFn;

var Socket = function () {
  this.id = ++Socket.id;
  this.broadcast = Broadcast.bind(this);
  Room.get('all')._join(this);
};

Socket.prototype.emit = function (event, a, b, c) {
  lastMessage[this.id] = [event, a, b, c];
  emitFn.apply(this, arguments);
};

Socket.prototype.namespace = function (name) {
  return new Namespace(name, this);
};

Socket.prototype.last = function () {
  return lastMessage[this.id];
};

Socket.prototype.join = function (room) {
  Room.get(room)._join(this);
};

Socket.prototype.leave = function (room) {
  Room.get(room)._leave(this);
};

Socket.id = 0;

Socket.listen = function (fn) {
  emitFn = fn;
};

Socket.reset = function () {
  lastMessage = {};
  emitFn = function () {};
  Room.flush();
  Broadcast.init(Room);
};

Socket.reset();

module.exports = Socket;