[英]NodeJS, WebSockets, upgrade head
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.