コード例 #1
0
ファイル: Socket.js プロジェクト: BillTheBest/fast-tcp
Socket.prototype._emit = function(event, data, opts, cb) {
  var socketIds = ut.isArray(opts.sockets) ? opts.sockets : [];
  var rooms = ut.isArray(opts.rooms) ? opts.rooms : [];
  var broadcast = ut.isBoolean(opts.broadcast) ? opts.broadcast : false;

  if (broadcast) {
    this._send(event, data, Serializer.MT_DATA_BROADCAST);
  }

  if (socketIds.length > 0) {
    this._send(socketIds.join(',') + ':' + event, data, Serializer.MT_DATA_TO_SOCKET);
  }

  if (rooms.length > 0) {
    this._send(rooms.join(',') + ':' + event, data, Serializer.MT_DATA_TO_ROOM);
  }

  if (socketIds.length + rooms.length === 0 && !broadcast) {
    opts = {};
    var mt = Serializer.MT_DATA;

    if (cb !== null) {
      opts.cb = cb;
      mt = Serializer.MT_DATA_WITH_ACK;
    }

    this._send(event, data, mt, opts);
  }
};
コード例 #2
0
ファイル: SocketServ.js プロジェクト: BillTheBest/fast-tcp
SocketServ.prototype._emit = function(event, data, opts, cb) {
  var socketIds = ut.isArray(opts.sockets) ? opts.sockets : [];
  var rooms = ut.isArray(opts.rooms) ? opts.rooms : [];
  var broadcast = ut.isBoolean(opts.broadcast) ? opts.broadcast : false;

  if (broadcast) {
    this._server.emit(event, data, { except: [this.id] });
  }

  if (socketIds.length > 0) {
    this._server.emit(event, data, { sockets: socketIds });
  }

  if (rooms.length > 0) {
    this._server.emit(event, data, { rooms: rooms, except: [this.id] });
  }

  if (socketIds.length + rooms.length === 0 && !broadcast) {
    opts = {};
    var mt = Serializer.MT_DATA;

    if (cb !== null) {
      opts.cb = cb;
      mt = Serializer.MT_DATA_WITH_ACK;
    }

    this._send(event, data, mt, opts);
  }
};