Socket.io 套接字未使用express初始化

Socket.io 套接字未使用express初始化,socket.io,Socket.io,下面是在nodejs(16.x)/express(4.17)上运行的http服务器上初始化socket.io 4.0的代码片段: 这是一个非常标准的服务器初始化代码,但是没有Socketio服务器初始化的控制台输出或任何错误。以下是生成的io的输出: io : <ref *1> Server { _events: [Object: null prototype] {}, _eventsCount: 0, _maxListeners: undefined, _nsps

下面是在nodejs(16.x)/express(4.17)上运行的http服务器上初始化socket.io 4.0的代码片段:

这是一个非常标准的服务器初始化代码,但是没有
Socketio服务器初始化的控制台输出或任何错误。以下是生成的
io
的输出:

io :  <ref *1> Server {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  _nsps: Map(1) {
    '/' => Namespace {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      sockets: Map(0) {},
      _fns: [],
      _ids: 0,
      server: [Circular *1],
      name: '/',
      adapter: [Adapter],
      [Symbol(kCapture)]: false
    }
  },
  parentNsps: Map(0) {},
  _path: '/socket.io',
  clientPathRegex: /^\/socket\.io\/socket\.io(\.min|\.msgpack\.min)?\.js(\.map)?$/,
  _connectTimeout: 45000,
  _serveClient: true,
  _parser: {
    protocol: 5,
    PacketType: {
      '0': 'CONNECT',
      '1': 'DISCONNECT',
      '2': 'EVENT',
      '3': 'ACK',
      '4': 'CONNECT_ERROR',
      '5': 'BINARY_EVENT',
      '6': 'BINARY_ACK',
      CONNECT: 0,
      DISCONNECT: 1,
      EVENT: 2,
      ACK: 3,
      CONNECT_ERROR: 4,
      BINARY_EVENT: 5,
      BINARY_ACK: 6
    },
    Encoder: [class Encoder],
    Decoder: [class Decoder extends Emitter]
  },
  encoder: Encoder {},
  _adapter: [class Adapter extends EventEmitter],
  sockets: <ref *2> Namespace {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    sockets: Map(0) {},
    _fns: [],
    _ids: 0,
    server: [Circular *1],
    name: '/',
    adapter: Adapter {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      nsp: [Circular *2],
      rooms: Map(0) {},
      sids: Map(0) {},
      encoder: Encoder {},
      [Symbol(kCapture)]: false
    },
    [Symbol(kCapture)]: false
  },
  opts: {},
  eio: Server {
    _events: [Object: null prototype] {
      connection: [Function: bound onconnection]
    },
    _eventsCount: 1,
    _maxListeners: undefined,
    clients: {},
    clientsCount: 0,
    opts: {
      wsEngine: [class WebSocketServer extends EventEmitter],
      pingTimeout: 20000,
      pingInterval: 25000,
      upgradeTimeout: 10000,
      maxHttpBufferSize: 1000000,
      transports: [Array],
      allowUpgrades: true,
      httpCompression: [Object],
      cors: false,
      allowEIO3: false,
      path: '/socket.io'
    },
    ws: WebSocketServer {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      options: [Object],
      [Symbol(kCapture)]: false
    },
    [Symbol(kCapture)]: false
  },
  httpServer: Server {
    maxHeaderSize: undefined,
    insecureHTTPParser: undefined,
    _events: [Object: null prototype] {
      connection: [Function: connectionListener],
      close: [Function: bound close],
      listening: [Function: bound init],
      upgrade: [Function (anonymous)],
      request: [Function (anonymous)]
    },
    _eventsCount: 5,
    _maxListeners: undefined,
    _connections: 0,
    _handle: null,
    _usingWorkers: false,
    _workers: [],
    _unref: false,
    allowHalfOpen: true,
    pauseOnConnect: false,
    httpAllowHalfOpen: false,
    timeout: 0,
    keepAliveTimeout: 5000,
    maxHeadersCount: null,
    headersTimeout: 60000,
    requestTimeout: 0,
    [Symbol(IncomingMessage)]: [Function: IncomingMessage],
    [Symbol(ServerResponse)]: [Function: ServerResponse],
    [Symbol(kCapture)]: false,
    [Symbol(async_id_symbol)]: -1
  },
  engine: Server {
    _events: [Object: null prototype] {
      connection: [Function: bound onconnection]
    },
    _eventsCount: 1,
    _maxListeners: undefined,
    clients: {},
    clientsCount: 0,
    opts: {
      wsEngine: [class WebSocketServer extends EventEmitter],
      pingTimeout: 20000,
      pingInterval: 25000,
      upgradeTimeout: 10000,
      maxHttpBufferSize: 1000000,
      transports: [Array],
      allowUpgrades: true,
      httpCompression: [Object],
      cors: false,
      allowEIO3: false,
      path: '/socket.io'
    },
    ws: WebSocketServer {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      options: [Object],
      [Symbol(kCapture)]: false
    },
    [Symbol(kCapture)]: false
  },
  [Symbol(kCapture)]: false
}
io:Server{
_事件:[对象:空原型]{},
_事件提示:0,
_maxListeners:未定义,
_nsps:Map(1){
“/”=>名称空间{
_事件:[对象:空原型]{},
_事件提示:0,
_maxListeners:未定义,
套接字:映射(0){},
_fns:[],
_ID:0,
服务器:[循环*1],
名称:“/”,
适配器:[适配器],
[符号(kCapture)]:false
}
},
parentNsps:Map(0){},
_路径:'/socket.io',
clientPathRegex:/^\/socket\.io\/socket\.io(\.min \.msgpack\.min)\.js(\.map)$/,在,
_连接超时:45000,
_服务员:是的,
_解析器:{
议定书:5,
包装类型:{
“0”:“连接”,
“1”:“断开连接”,
“2”:“事件”,
“3”:“确认”,
“4”:“连接错误”,
“5”:“二进制事件”,
'6':'BINARY_ACK',
连接:0,
断开连接:1,
活动:2,
ACK:3,
连接错误:4,
二元事件:5,
二进制应答:6
},
编码器:[类编码器],
解码器:[类解码器扩展发射器]
},
编码器:编码器{},
_适配器:[类适配器扩展EventEmitter],
套接字:命名空间{
_事件:[对象:空原型]{},
_事件提示:0,
_maxListeners:未定义,
套接字:映射(0){},
_fns:[],
_ID:0,
服务器:[循环*1],
名称:“/”,
适配器:适配器{
_事件:[对象:空原型]{},
_事件提示:0,
_maxListeners:未定义,
nsp:[循环*2],
房间:地图(0){},
小岛屿发展中国家:地图(0){},
编码器:编码器{},
[符号(kCapture)]:false
},
[符号(kCapture)]:false
},
选项:{},
eio:服务器{
_事件:[对象:空原型]{
连接:[函数:绑定到连接]
},
_事件提示:1,
_maxListeners:未定义,
客户:{},
客户:0,
选择:{
wsEngine:[类WebSocketServer扩展EventEmitter],
平均超时时间:20000,
时间间隔:25000,
升级超时:10000,
maxHttpBufferSize:1000000,
传输:[数组],
allowUpgrades:是的,
httpCompression:[对象],
科尔斯:错,
allowEIO3:错,
路径:'/socket.io'
},
ws:WebSocketServer{
_事件:[对象:空原型]{},
_事件提示:0,
_maxListeners:未定义,
选项:[对象],
[符号(kCapture)]:false
},
[符号(kCapture)]:false
},
httpServer:Server{
maxHeaderSize:未定义,
不安全的HttpParser:未定义,
_事件:[对象:空原型]{
连接:[函数:connectionListener],
关闭:[函数:绑定关闭],
侦听:[函数:绑定初始化],
升级:[功能(匿名)],
请求:[功能(匿名)]
},
_活动日期:5,,
_maxListeners:未定义,
_连接:0,
_句柄:null,
_使用工人:错误,
_工人:[],
_unref:错,
AllowAlfOpen:是的,
pauseOnConnect:false,
HttpAllowWhalfOpen:错误,
超时:0,
keepAliveTimeout:5000,
maxHeadersCount:null,
校长人数:60000,
请求超时:0,
[符号(输入消息)]:[功能:输入消息],
[符号(服务器响应)]:[功能:服务器响应],
[符号(kCapture)]:假,
[符号(异步\u id\u符号)]:-1
},
引擎:服务器{
_事件:[对象:空原型]{
连接:[函数:绑定到连接]
},
_事件提示:1,
_maxListeners:未定义,
客户:{},
客户:0,
选择:{
wsEngine:[类WebSocketServer扩展EventEmitter],
平均超时时间:20000,
时间间隔:25000,
升级超时:10000,
maxHttpBufferSize:1000000,
传输:[数组],
allowUpgrades:是的,
httpCompression:[对象],
科尔斯:错,
allowEIO3:错,
路径:'/socket.io'
},
ws:WebSocketServer{
_事件:[对象:空原型]{},
_事件提示:0,
_maxListeners:未定义,
选项:[对象],
[符号(kCapture)]:false
},
[符号(kCapture)]:false
},
[符号(kCapture)]:false
}

上面的
io
似乎正常。这里缺少什么?

该问题是由客户端套接字创建错误引起的。当客户端成功生成客户端时,相应地,服务器端也会创建套接字实例。
io :  <ref *1> Server {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  _nsps: Map(1) {
    '/' => Namespace {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      sockets: Map(0) {},
      _fns: [],
      _ids: 0,
      server: [Circular *1],
      name: '/',
      adapter: [Adapter],
      [Symbol(kCapture)]: false
    }
  },
  parentNsps: Map(0) {},
  _path: '/socket.io',
  clientPathRegex: /^\/socket\.io\/socket\.io(\.min|\.msgpack\.min)?\.js(\.map)?$/,
  _connectTimeout: 45000,
  _serveClient: true,
  _parser: {
    protocol: 5,
    PacketType: {
      '0': 'CONNECT',
      '1': 'DISCONNECT',
      '2': 'EVENT',
      '3': 'ACK',
      '4': 'CONNECT_ERROR',
      '5': 'BINARY_EVENT',
      '6': 'BINARY_ACK',
      CONNECT: 0,
      DISCONNECT: 1,
      EVENT: 2,
      ACK: 3,
      CONNECT_ERROR: 4,
      BINARY_EVENT: 5,
      BINARY_ACK: 6
    },
    Encoder: [class Encoder],
    Decoder: [class Decoder extends Emitter]
  },
  encoder: Encoder {},
  _adapter: [class Adapter extends EventEmitter],
  sockets: <ref *2> Namespace {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    sockets: Map(0) {},
    _fns: [],
    _ids: 0,
    server: [Circular *1],
    name: '/',
    adapter: Adapter {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      nsp: [Circular *2],
      rooms: Map(0) {},
      sids: Map(0) {},
      encoder: Encoder {},
      [Symbol(kCapture)]: false
    },
    [Symbol(kCapture)]: false
  },
  opts: {},
  eio: Server {
    _events: [Object: null prototype] {
      connection: [Function: bound onconnection]
    },
    _eventsCount: 1,
    _maxListeners: undefined,
    clients: {},
    clientsCount: 0,
    opts: {
      wsEngine: [class WebSocketServer extends EventEmitter],
      pingTimeout: 20000,
      pingInterval: 25000,
      upgradeTimeout: 10000,
      maxHttpBufferSize: 1000000,
      transports: [Array],
      allowUpgrades: true,
      httpCompression: [Object],
      cors: false,
      allowEIO3: false,
      path: '/socket.io'
    },
    ws: WebSocketServer {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      options: [Object],
      [Symbol(kCapture)]: false
    },
    [Symbol(kCapture)]: false
  },
  httpServer: Server {
    maxHeaderSize: undefined,
    insecureHTTPParser: undefined,
    _events: [Object: null prototype] {
      connection: [Function: connectionListener],
      close: [Function: bound close],
      listening: [Function: bound init],
      upgrade: [Function (anonymous)],
      request: [Function (anonymous)]
    },
    _eventsCount: 5,
    _maxListeners: undefined,
    _connections: 0,
    _handle: null,
    _usingWorkers: false,
    _workers: [],
    _unref: false,
    allowHalfOpen: true,
    pauseOnConnect: false,
    httpAllowHalfOpen: false,
    timeout: 0,
    keepAliveTimeout: 5000,
    maxHeadersCount: null,
    headersTimeout: 60000,
    requestTimeout: 0,
    [Symbol(IncomingMessage)]: [Function: IncomingMessage],
    [Symbol(ServerResponse)]: [Function: ServerResponse],
    [Symbol(kCapture)]: false,
    [Symbol(async_id_symbol)]: -1
  },
  engine: Server {
    _events: [Object: null prototype] {
      connection: [Function: bound onconnection]
    },
    _eventsCount: 1,
    _maxListeners: undefined,
    clients: {},
    clientsCount: 0,
    opts: {
      wsEngine: [class WebSocketServer extends EventEmitter],
      pingTimeout: 20000,
      pingInterval: 25000,
      upgradeTimeout: 10000,
      maxHttpBufferSize: 1000000,
      transports: [Array],
      allowUpgrades: true,
      httpCompression: [Object],
      cors: false,
      allowEIO3: false,
      path: '/socket.io'
    },
    ws: WebSocketServer {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      options: [Object],
      [Symbol(kCapture)]: false
    },
    [Symbol(kCapture)]: false
  },
  [Symbol(kCapture)]: false
}