123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- /**
- * Module dependencies.
- */
- var parser = require('socket.io-parser');
- var Emitter = require('component-emitter');
- var toArray = require('to-array');
- var on = require('./on');
- var bind = require('component-bind');
- var debug = require('debug')('socket.io-client:socket');
- var parseqs = require('parseqs');
- var hasBin = require('has-binary2');
- /**
- * Module exports.
- */
- module.exports = exports = Socket;
- /**
- * Internal events (blacklisted).
- * These events can't be emitted by the user.
- *
- * @api private
- */
- var events = {
- connect: 1,
- connect_error: 1,
- connect_timeout: 1,
- connecting: 1,
- disconnect: 1,
- error: 1,
- reconnect: 1,
- reconnect_attempt: 1,
- reconnect_failed: 1,
- reconnect_error: 1,
- reconnecting: 1,
- ping: 1,
- pong: 1
- };
- /**
- * Shortcut to `Emitter#emit`.
- */
- var emit = Emitter.prototype.emit;
- /**
- * `Socket` constructor.
- *
- * @api public
- */
- function Socket (io, nsp, opts) {
- this.io = io;
- this.nsp = nsp;
- this.json = this; // compat
- this.ids = 0;
- this.acks = {};
- this.receiveBuffer = [];
- this.sendBuffer = [];
- this.connected = false;
- this.disconnected = true;
- this.flags = {};
- if (opts && opts.query) {
- this.query = opts.query;
- }
- if (this.io.autoConnect) this.open();
- }
- /**
- * Mix in `Emitter`.
- */
- Emitter(Socket.prototype);
- /**
- * Subscribe to open, close and packet events
- *
- * @api private
- */
- Socket.prototype.subEvents = function () {
- if (this.subs) return;
- var io = this.io;
- this.subs = [
- on(io, 'open', bind(this, 'onopen')),
- on(io, 'packet', bind(this, 'onpacket')),
- on(io, 'close', bind(this, 'onclose'))
- ];
- };
- /**
- * "Opens" the socket.
- *
- * @api public
- */
- Socket.prototype.open =
- Socket.prototype.connect = function () {
- if (this.connected) return this;
- this.subEvents();
- if (!this.io.reconnecting) this.io.open(); // ensure open
- if ('open' === this.io.readyState) this.onopen();
- this.emit('connecting');
- return this;
- };
- /**
- * Sends a `message` event.
- *
- * @return {Socket} self
- * @api public
- */
- Socket.prototype.send = function () {
- var args = toArray(arguments);
- args.unshift('message');
- this.emit.apply(this, args);
- return this;
- };
- /**
- * Override `emit`.
- * If the event is in `events`, it's emitted normally.
- *
- * @param {String} event name
- * @return {Socket} self
- * @api public
- */
- Socket.prototype.emit = function (ev) {
- if (events.hasOwnProperty(ev)) {
- emit.apply(this, arguments);
- return this;
- }
- var args = toArray(arguments);
- var packet = {
- type: (this.flags.binary !== undefined ? this.flags.binary : hasBin(args)) ? parser.BINARY_EVENT : parser.EVENT,
- data: args
- };
- packet.options = {};
- packet.options.compress = !this.flags || false !== this.flags.compress;
- // event ack callback
- if ('function' === typeof args[args.length - 1]) {
- debug('emitting packet with ack id %d', this.ids);
- this.acks[this.ids] = args.pop();
- packet.id = this.ids++;
- }
- if (this.connected) {
- this.packet(packet);
- } else {
- this.sendBuffer.push(packet);
- }
- this.flags = {};
- return this;
- };
- /**
- * Sends a packet.
- *
- * @param {Object} packet
- * @api private
- */
- Socket.prototype.packet = function (packet) {
- packet.nsp = this.nsp;
- this.io.packet(packet);
- };
- /**
- * Called upon engine `open`.
- *
- * @api private
- */
- Socket.prototype.onopen = function () {
- debug('transport is open - connecting');
- // write connect packet if necessary
- if ('/' !== this.nsp) {
- if (this.query) {
- var query = typeof this.query === 'object' ? parseqs.encode(this.query) : this.query;
- debug('sending connect packet with query %s', query);
- this.packet({type: parser.CONNECT, query: query});
- } else {
- this.packet({type: parser.CONNECT});
- }
- }
- };
- /**
- * Called upon engine `close`.
- *
- * @param {String} reason
- * @api private
- */
- Socket.prototype.onclose = function (reason) {
- debug('close (%s)', reason);
- this.connected = false;
- this.disconnected = true;
- delete this.id;
- this.emit('disconnect', reason);
- };
- /**
- * Called with socket packet.
- *
- * @param {Object} packet
- * @api private
- */
- Socket.prototype.onpacket = function (packet) {
- var sameNamespace = packet.nsp === this.nsp;
- var rootNamespaceError = packet.type === parser.ERROR && packet.nsp === '/';
- if (!sameNamespace && !rootNamespaceError) return;
- switch (packet.type) {
- case parser.CONNECT:
- this.onconnect();
- break;
- case parser.EVENT:
- this.onevent(packet);
- break;
- case parser.BINARY_EVENT:
- this.onevent(packet);
- break;
- case parser.ACK:
- this.onack(packet);
- break;
- case parser.BINARY_ACK:
- this.onack(packet);
- break;
- case parser.DISCONNECT:
- this.ondisconnect();
- break;
- case parser.ERROR:
- this.emit('error', packet.data);
- break;
- }
- };
- /**
- * Called upon a server event.
- *
- * @param {Object} packet
- * @api private
- */
- Socket.prototype.onevent = function (packet) {
- var args = packet.data || [];
- debug('emitting event %j', args);
- if (null != packet.id) {
- debug('attaching ack callback to event');
- args.push(this.ack(packet.id));
- }
- if (this.connected) {
- emit.apply(this, args);
- } else {
- this.receiveBuffer.push(args);
- }
- };
- /**
- * Produces an ack callback to emit with an event.
- *
- * @api private
- */
- Socket.prototype.ack = function (id) {
- var self = this;
- var sent = false;
- return function () {
- // prevent double callbacks
- if (sent) return;
- sent = true;
- var args = toArray(arguments);
- debug('sending ack %j', args);
- self.packet({
- type: hasBin(args) ? parser.BINARY_ACK : parser.ACK,
- id: id,
- data: args
- });
- };
- };
- /**
- * Called upon a server acknowlegement.
- *
- * @param {Object} packet
- * @api private
- */
- Socket.prototype.onack = function (packet) {
- var ack = this.acks[packet.id];
- if ('function' === typeof ack) {
- debug('calling ack %s with %j', packet.id, packet.data);
- ack.apply(this, packet.data);
- delete this.acks[packet.id];
- } else {
- debug('bad ack %s', packet.id);
- }
- };
- /**
- * Called upon server connect.
- *
- * @api private
- */
- Socket.prototype.onconnect = function () {
- this.connected = true;
- this.disconnected = false;
- this.emitBuffered();
- this.emit('connect');
- };
- /**
- * Emit buffered events (received and emitted).
- *
- * @api private
- */
- Socket.prototype.emitBuffered = function () {
- var i;
- for (i = 0; i < this.receiveBuffer.length; i++) {
- emit.apply(this, this.receiveBuffer[i]);
- }
- this.receiveBuffer = [];
- for (i = 0; i < this.sendBuffer.length; i++) {
- this.packet(this.sendBuffer[i]);
- }
- this.sendBuffer = [];
- };
- /**
- * Called upon server disconnect.
- *
- * @api private
- */
- Socket.prototype.ondisconnect = function () {
- debug('server disconnect (%s)', this.nsp);
- this.destroy();
- this.onclose('io server disconnect');
- };
- /**
- * Called upon forced client/server side disconnections,
- * this method ensures the manager stops tracking us and
- * that reconnections don't get triggered for this.
- *
- * @api private.
- */
- Socket.prototype.destroy = function () {
- if (this.subs) {
- // clean subscriptions to avoid reconnections
- for (var i = 0; i < this.subs.length; i++) {
- this.subs[i].destroy();
- }
- this.subs = null;
- }
- this.io.destroy(this);
- };
- /**
- * Disconnects the socket manually.
- *
- * @return {Socket} self
- * @api public
- */
- Socket.prototype.close =
- Socket.prototype.disconnect = function () {
- if (this.connected) {
- debug('performing disconnect (%s)', this.nsp);
- this.packet({ type: parser.DISCONNECT });
- }
- // remove socket from pool
- this.destroy();
- if (this.connected) {
- // fire events
- this.onclose('io client disconnect');
- }
- return this;
- };
- /**
- * Sets the compress flag.
- *
- * @param {Boolean} if `true`, compresses the sending data
- * @return {Socket} self
- * @api public
- */
- Socket.prototype.compress = function (compress) {
- this.flags.compress = compress;
- return this;
- };
- /**
- * Sets the binary flag
- *
- * @param {Boolean} whether the emitted data contains binary
- * @return {Socket} self
- * @api public
- */
- Socket.prototype.binary = function (binary) {
- this.flags.binary = binary;
- return this;
- };
|