javascript - tutorial - socket.io chat
node.js webserver中的Write After End錯誤 (2)
由於“寫完後”錯誤,我正在努力處理我的node.js愛好項目。 我創建了一個node.js網絡服務器,除其他外,使用以下代碼將從HTML頁面接收的命令發送到另一個進程:
var netSocket = require('net').Socket();
netSocket.connect(9090);
netSocket.write(messages);
netSocket.end();
這一直有效,直到流量開始增加(即發送的消息量和消息的大小)。 此時我收到以下錯誤:
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世界中的一個完整的新手,我真的很感激有關如何解決這個問題的任何提示。
另請注意,Web服務器通過SSL提供頁面(如果有任何不同)。
感謝您抽時間閱讀!
標記
https://code.i-harness.com
node.js是一個非阻塞的異步平台。
在你的情況下,
netSocket.write(messages);
是一個Async方法,因此在'write'完成之前調用netSocket.end()。
正確的用途是:
netSocket.write(messages, function(err) { netSocket.end(); });
這裡的第二個參數是一個回調函數,一旦'write'方法完成它的工作就會被調用。
我建議您閱讀/觀看有關node.js,異步樣式和回調的更多信息。
這是一個很好的起點: https://www.youtube.com/watch?v=GJmFG4ffJZU : https://www.youtube.com/watch?v=GJmFG4ffJZU
當然還有關於網絡套接字的node.js API文檔 。
希望它有幫助:)
我有類似的節點模塊壓縮問題,在我將其更新到最新版本1.6後,問題得到了解決
npm install [email protected]