简体   繁体   English

错误 [ERR_STREAM_WRITE_AFTER_END]:在结束后写入 [npm-request with socket.io]

[英]Error [ERR_STREAM_WRITE_AFTER_END]: write after end [npm-request with socket.io]

On my server side I have socket server listening and in my own laptop I have socket.io-client service and whenever I turn on both they are connecting.在我的服务器端,我有套接字服务器侦听,在我自己的笔记本电脑中,我有 socket.io-client 服务,每当我打开它们时,它们都在连接。

And when other people request to my server, server sends that request via socket to my laptop and my laptop gets data from localhost using npm-request and gives back the returned data to the server then server show that information to the client.当其他人向我的服务器请求时,服务器通过套接字将该请求发送到我的笔记本电脑,我的笔记本电脑使用 npm-request 从本地主机获取数据并将返回的数据返回给服务器,然后服务器将该信息显示给客户端。

And here is the error on my server side:这是我服务器端的错误:

/*

throw er; // Unhandled 'error' event
      ^

**Error [ERR_STREAM_WRITE_AFTER_END]: write after end**
    at write_ (_http_outgoing.js:572:17)
    at ServerResponse.write (_http_outgoing.js:567:10)
    at Socket.<anonymous> (/public_html/api/index.js:37:9)
    at Socket.emit (events.js:187:15)
    at /public_html/api/node_modules/socket.io/lib/socket.js:528:12
    at process._tickCallback (internal/process/next_tick.js:61:11)
Emitted 'error' event at:
    at writeAfterEndNT (_http_outgoing.js:634:7)
    at process._tickCallback (internal/process/next_tick.js:63:19)

    */

server side code:服务器端代码:

var http = require('http');
var fs = require('fs');
var socket_g=null, numOfUsers=0;
var url = require('url'),qs = require('querystring');

var server = http.createServer(function(req, res) {
    if(numOfUsers!=0){
          var urlParts = url.parse(req.url, true),
         urlParams = urlParts.query, 
         urlPathname = urlParts.pathname,
         body = '',
         reqInfo = {};

          req.on('data', function (data) {
            body += data; 
          });

          req.on('end', function () {
            reqInfo.urlPathname = urlPathname; 
            reqInfo.urlParams = urlParams; 
            reqInfo.body = qs.parse(body); 
            reqInfo.urlParts = urlParts;
            console.log(reqInfo.urlPathname)
              socket_g.emit('event', { "path": reqInfo.urlPathname});
          });

        socket_g.on('data',function(data){
              console.log(data.c)
              if(data.c=='application/x-httpd-php' || data.c=='/'){
                    res.writeHead(200, { 'Content-Type': 'text/html' });
                    res.write(data.data);
                    res.end();
              }else{
                    res.writeHead(200, { 'Content-Type': data.c });
                    res.write(data.data);
                    res.end();
              }         
        });
    }else{
        res.writeHead(200, { 'Content-Type': 'text/html' });
            res.end("<h2>There is no client connected!</h2>");
    }
});

var io = require('socket.io').listen(server);
io.sockets.setMaxListeners(0);

io.sockets.on('connection', function (socket) {
    /*console msg*/
    console.log('user connected!');
    /*console msg*/
    socket.setMaxListeners(0);
    numOfUsers++;
    socket_g=socket;
    socket.on('disconnect', function(){
        numOfUsers++;
        /*console msg*/
        console.log('user disconnected');
        /*console msg*/
    });
});


server.listen(3333);

And belove my client side code, which is working on my laptop爱我的客户端代码,它在我的笔记本电脑上工作

 var socket = require('socket.io-client')('http://*.*.*.*:3333/'); var http=require("http"); var r = require('request'); var mime = require('mime'); var path = require('path'); socket.on('connect', function(){ console.log("connected"); }); socket.on('event', function(data){ console.log(mime.getType(path.extname(data.path).substr(1))); var contentType=mime.getType(path.extname(data.path).substr(1)); r.get({url: "http://localhost/check"+data.path}, function(error, response, body){ //console.log(body); if(contentType){ socket.emit('data', { "data": body,'c':contentType }); } }); }); socket.on('disconnect', function(){ console.log("disconnected") });

Problem:问题:

You are listening to the 'data' event even after returning the response.即使在返回响应后,您仍在收听 'data' 事件。

Solution:解决方案:

Use .once to listen to the event for one time for that one particular request.使用.once为该特定请求侦听一次事件。

socket_g.once("data", function(data) { 

I had a similiar issue.我有一个类似的问题。 My solution was to:我的解决方案是:

  1. Check current version of node nvm list => in my case was v9.5.0检查当前版本的节点nvm list => 在我的情况下是v9.5.0
  2. Using nvm change to another node version nvm use 10.10.0使用 nvm 更改为另一个节点版本nvm use 10.10.0
  3. Uninstall that node version nvm uninstall 9.5.0卸载那个节点版本nvm uninstall 9.5.0
  4. Reinstall that node version nvm install 9.5.0重新安装那个节点版本nvm install 9.5.0
  5. Change back to that node version nvm use 9.5.0改回那个节点版本nvm use 9.5.0

And it was solved!并且解决了!

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

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