简体   繁体   English

NodeJS,WebSockets,升级头

[英]NodeJS, WebSockets, upgrade head

我正在玩NodeJS和WebSockets,有一个带有head参数的升级事件 ,正如我从这里理解的那样,基本上data that directly trails the headers ,但对于我的用例,它总是空的,所以我真的不知道是什么这意味着,如果有人能提供升级事件中的数据参数不为空的简单用例,我会很高兴。

Looking at the source that emits that upgrade event in the node repo, you'll see the following implementation: 查看在节点repo中发出该升级事件的源代码 ,您将看到以下实现:

  function socketOnData(d) {
    assert(!socket._paused);
    debug('SERVER socketOnData %d', d.length);
    var ret = parser.execute(d);

    onParserExecuteCommon(ret, d);
  }

  function onParserExecute(ret, d) {
    debug('SERVER socketOnParserExecute %d', ret);
    onParserExecuteCommon(ret, undefined);
  }

  function onParserExecuteCommon(ret, d) {
    if (ret instanceof Error) {
      debug('parse error');
      socket.destroy(ret);
    } else if (parser.incoming && parser.incoming.upgrade) {
      // Upgrade or CONNECT
      var bytesParsed = ret;
      var req = parser.incoming;
      debug('SERVER upgrade or connect', req.method);

      if (!d)
        d = parser.getCurrentBuffer();

      socket.removeListener('data', socketOnData);
      socket.removeListener('end', socketOnEnd);
      socket.removeListener('close', serverSocketCloseListener);
      unconsume(parser, socket);
      parser.finish();
      freeParser(parser, req, null);
      parser = null;

      var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
      if (EventEmitter.listenerCount(self, eventName) > 0) {
        debug('SERVER have listener for %s', eventName);
        var bodyHead = d.slice(bytesParsed, d.length);

        // TODO(isaacs): Need a way to reset a stream to fresh state
        // IE, not flowing, and not explicitly paused.
        socket._readableState.flowing = null;
        self.emit(eventName, req, socket, bodyHead);
...

The passed parameter, bodyHead , reflects the data from the passed parameter d on the socketOnData function, which is the default socket data event handler. 传递的参数bodyHead反映了socketOnData函数上传递的参数d的数据,该函数是默认的套接字data事件处理程序。 On the other hand, it could be undefined if the onParserExecuteCommon was called from onParserExecute . 另一方面,如果从onParserExecuteCommon调用onParserExecute ,则可能undefined I'd have to look through the source more to understand which cases would apply depending on how your server is implemented. 我必须更多地查看源代码,以了解哪些情况适用,具体取决于服务器的实现方式。 Maybe you could enable the debug logs to see which methods are being called. 也许您可以启用调试日志以查看正在调用的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM