Javascript 在 node.js 网络服务器中写入后结束错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27769842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write After End error in node.js webserver
提问by Mark
I am struggling with my node.js hobby project due to a "write after end" Error. I have a created a node.js webserver that amongst other things, sends commands received from a HTML page onwards to another process using the following code:
由于“结束后写入”错误,我正在为我的 node.js 爱好项目苦苦挣扎。我创建了一个 node.js 网络服务器,它使用以下代码将从 HTML 页面接收到的命令发送到另一个进程:
var netSocket = require('net').Socket();
netSocket.connect(9090);
netSocket.write(messages);
netSocket.end();
This works until the traffic starts to increase (i.e. the amount of messages being sent and or the size of the messages). At this point I get the following error:
这一直有效,直到流量开始增加(即发送的消息量和/或消息的大小)。此时我收到以下错误:
Error: write after end
at writeAfterEnd (_stream_writable.js:132:12)
at Socket.Writable.write (_stream_writable.js:180:5)
at Socket.write (net.js:615:40)
at Socket.<anonymous> (/Users/mark/Documents/GitHub Repos/voice_controlled_zumo/speech_module/web_server_HTTPS.js:66:15)
at Socket.emit (events.js:95:17)
at Socket.onevent (/Users/mark/Documents/GitHub Repos/voice_controlled_zumo/node_modules/socket.io/lib/socket.js:327:8)
at Socket.onpacket (/Users/mark/Documents/GitHub Repos/voice_controlled_zumo/node_modules/socket.io/lib/socket.js:287:12)
at Client.ondecoded (/Users/mark/Documents/GitHub Repos/voice_controlled_zumo/node_modules/socket.io/lib/client.js:193:14)
at Decoder.Emitter.emit (/Users/mark/Documents/GitHub Repos/voice_controlled_zumo/node_modules/socket.io/node_modules/socket.io-parser/node_modules/component-emitter/index.js:134:20)
My guess is that the server at 9090 is being overwhelmed by the amount of traffic, giving rise to the error. As a complete newbie in the node.js world I'd really appreciate any hints for how I could resolve this issue.
我的猜测是 9090 的服务器被流量淹没,导致错误。作为 node.js 世界的完全新手,我非常感谢有关如何解决此问题的任何提示。
Note also that the webserver is serving pages over SSL (in case that makes any difference).
另请注意,网络服务器通过 SSL 提供页面(以防万一)。
Thanks for taking the time to read this!
感谢您抽时间阅读!
Mark
标记
回答by Ron Bar
node.js is a non-blocking async platform.
node.js 是一个非阻塞异步平台。
In your case,
在你的情况下,
netSocket.write(messages);
is an Async method, therefore netSocket.end() is called before 'write' is complete.
是一种异步方法,因此在“写入”完成之前调用 netSocket.end()。
the correct use would be:
正确的用法是:
netSocket.write(messages, function(err) { netSocket.end(); });
The second argument here is a callback function that will be called once the 'write' method finishes its job.
这里的第二个参数是一个回调函数,一旦“write”方法完成它的工作就会被调用。
I would recommend you read/watch more about node.js, async styles and callbacks.
我建议您阅读/观看有关 node.js、异步样式和回调的更多信息。
here is a great place to start: https://www.youtube.com/watch?v=GJmFG4ffJZU
这是一个很好的起点:https: //www.youtube.com/watch?v=GJmFG4ffJZU
And of course the node.js API docsregarding net sockets.
当然还有关于网络套接字的 node.js API 文档。
Hope it helped :)
希望它有所帮助:)
回答by Peter Lyons
First, I think there is misinformation in another answer about socket.write()and socket.end(). It is perfectly normal and OK to do them back to back in the same tick:
首先,我认为关于socket.write()and 的另一个答案中存在错误信息socket.end()。在同一个滴答声中背靠背执行它们是完全正常的:
socket.write(everythingIPlanToSend);
socket.end();
You do not need to provide a callback to write. The callback will tell you when the data has been fully flushed out the connection, but that is an optional notification that typical programs don't need to concern themselves with.
您不需要向 提供回调write。回调将告诉您数据何时完全刷新连接,但这是典型程序不需要关心的可选通知。
However, looking at your stack trace, I think your event handling control flow is broken in this manner. You have a socket.io client connected, and that emits events which you listen for. When those events fire you send them onward to your upstream server. Then you end the upstream socket connection. At that point, you must unbind (removeListener) your socket.io connection listener so that as more events arrive you don't attempt to send them up the connection you have already closed.
但是,查看您的堆栈跟踪,我认为您的事件处理控制流以这种方式被破坏。你有一个 socket.io 客户端连接,它发出你监听的事件。当这些事件触发时,您将它们转发到上游服务器。然后结束上游套接字连接。此时,您必须取消绑定 ( removeListener) 您的 socket.io 连接侦听器,以便随着更多事件到达您不会尝试将它们发送到您已经关闭的连接。
Another way to say it is whenever you call .end()on your upstream socket, you have to make sure that future incoming events from the browser do not make use of that same socket. OR you have to change your code to use the same upstream socket for all events from the corresponding browser socket (which is probably more efficient/correct), BUT in that case don't call .end()on it until the browser socket actually disconnects.
另一种说法是,无论何时调用.end()上游套接字,都必须确保将来来自浏览器的传入事件不会使用同一个套接字。或者您必须更改代码以对来自相应浏览器套接字的所有事件使用相同的上游套接字(这可能更有效/正确),但在这种情况下.end(),在浏览器套接字实际断开连接之前不要调用它。
回答by shacharsol
I had similar issue with node module compression, after i update it to the latest version 1.6, the issue was resolved
我在节点模块压缩方面遇到了类似的问题,将其更新到最新版本 1.6 后,问题已解决
npm install [email protected]

