summaryrefslogtreecommitdiff
path: root/node_modules/socket.io/lib/client.js
blob: d80067b32099145620bc5776ea37307743add129 (plain)
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
/**
 * Module dependencies.
 */

var parser = require('socket.io-client').parser
  , EventEmitter = require('events').EventEmitter

/**
 * Client constructor.
 *
 * @api public
 */

function Client (id, server) {
  this.id = id;
  this.acks = {};
  this.store = server.store;

  var self = this;

  store.subscribe(id, function (packet) {
    
  });

  store.subscribe(id + '.disconect', function () {
    self.onDisconnect();
  });
}

/**
 * Inherits from EventEmitter.
 */

Client.prototype.__proto__ = EventEmitter.prototype;

/**
 * Save reference to original `emit`.
 *
 * @api private
 */

Client.prototype._emit = Client.prototype.emit;

/**
 * Broadcast flag.
 *
 * @api public
 */

Client.prototype.__defineGetter__('broadcast', function () {
  this.flags.broadcast = true;
});

/**
 * JSON flag (deprecated)
 *
 * @api public
 */

Client.prototype.__defineGetter__('json', function () {
  this.flags.broadcast = true;
});

/**
 * Joins a group.
 *
 * @param {String} group
 * @return {Client} for chaining
 * @api public
 */

Client.prototype.join = function (group, fn) {
  if (!~this.subscriptions.indexOf(group)) {
    var self = this;
    this.subscriptions.push(group);
    this.store.addToGroup(group, this.sid, function (ev, args) {
      self.onGroupEvent(ev, args);
    }, fn);
  } else {
    fn && fn();
  }

  return this;
};

/**
 * Leaves a group.
 *
 * @return {Client} for chaining
 * @api public
 */

Client.prototype.leave = function (group) {
  var index = this.subscriptions.indexOf(group);
  if (~index) {
    this.subscriptions.splice(index, 1);
  }
  return this;
};

Client.prototype.disconnect = function () {
  if (this.socket) {
    this.socket.disconnect();
  } else {
    this.publish('disconnect');
  }
}

/**
 * Called upon disconnect.
 *
 * @api private
 */

Client.prototype.onDisconnect = function () {
  for (var i = 0, l = this.subscriptions; i < l; i++) {
    this.store.removeFromGroup(id, group, fn);
  }
};

/**
 * Registers ACK.
 */

Client.prototype.ack = function (fn, callback) {
  this.subscribe('ack');
};

/**
 * Emits an event.
 */

Client.prototype.emit = function () {
  var args = toArray(arguments), fn;

  if ('function' == typeof args[args.length - 1]) {
    fn = args.pop();
  }

  var data = args.shift();
  if (args.length) {
    data += '\n' + JSON.stringify(args);
  }

  if (fn) {
    this.ack(fn, function (id) {
      self.sendPacket('event', data, id);
    });
  } else {
    this.sendPacket('event', data);
  }

  return this;
};

/**
 * Sends a packet.
 */

Client.prototype.sendPacket = function (type, data, id) {
  var data = parser.encode({ type: type, data: data, id: id });

  if (this.server.sockets[id]) {
    this.server.sockets[id].write(data);
  }
};