Socket.io之Socket类

var Emitter = require('events').EventEmitter;
var parser = require('socket.io-parser');
var url = require('url');
var debug = require('debug')('socket.io:socket');

module.exports = exports = Socket;


//事件名数组
exports.events = [
  //错误事件
  'error',
  //连接事件
  'connect',
  //断开连接事件
  'disconnect',
  //正在断开
  'disconnecting',
  //新监听器
  'newListener',
  //移除监听器
  'removeListener'
];

var flags = [
  'json',
  'volatile',
  'broadcast'
];

var emit = Emitter.prototype.emit;

//顶层Socket构造函数,对应一个客户端对指定命名空间的连接
function Socket(nsp, client, query){
  //命名空间对象
  this.nsp = nsp;
  //服务对象
  this.server = nsp.server;
  //命名空间适配器
  this.adapter = this.nsp.adapter;
  //id,如果不是根命名空间,则为空间名称加上#加上客户端id,否则为客户端id
  this.id = nsp.name !== '/' ? nsp.name + '#' + client.id : client.id;
  //客户端
  this.client = client;
  //客户端连接,底层socket
  this.conn = client.conn;
  //已加入的房间名哈希,索引为房间名,值也是房间名
  this.rooms = {};
  //数据包id,与应答回调函数的哈希
  this.acks = {};
  //是否已连接
  this.conne

你可能感兴趣的:(socket.io)