node.js webserver中的Write After End错误 [英] Write After End error in node.js webserver

查看:230
本文介绍了node.js webserver中的Write After End错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于写完后错误,我正在努力处理我的node.js爱好项目。我创建了一个node.js网络服务器,除其他外,使用以下代码将从HTML页面接收的命令发送到另一个进程:

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:

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)

我的猜测是9090处的服务器被流量淹没,从而引发错误。作为node.js世界中的一个完全新手,我真的很感激我可以解决这个问题的任何提示。

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.

另请注意,网络服务器通过SSL提供服务(如果有任何区别)。

Note also that the webserver is serving pages over SSL (in case that makes any difference).

感谢您花时间阅读本文!

Thanks for taking the time to read this!

Mark

推荐答案

node.js是一个非阻塞的异步平台。

node.js is a non-blocking async platform.

在你的情况下,

netSocket.write(messages);

是一个异步方法,因此在'write'完成之前调用netSocket.end()。

is an Async method, therefore netSocket.end() is called before 'write' is complete.

正确的用法是:

netSocket.write(messages, function(err) { netSocket.end(); });

这里的第二个参数是一个回调函数,一旦'write'方法完成其工作就会被调用。

The second argument here is a callback function that will be called once the 'write' method finishes its job.

我建议您阅读/观看有关node.js,异步样式和回调的更多信息。

I would recommend you read/watch more about node.js, async styles and callbacks.

此处是一个很好的起点: https://www.youtube.com/watch?v=GJmFG4ffJZU

here is a great place to start: https://www.youtube.com/watch?v=GJmFG4ffJZU

当然关于net的node.js API docs 套接字。

And of course the node.js API docs regarding net sockets.

希望有所帮助:)

这篇关于node.js webserver中的Write After End错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆