子进程写入套接字 - localhost发送了无效的响应 [英] child process writing to socket - localhost sent an invalid response

查看:2918
本文介绍了子进程写入套接字 - localhost发送了无效的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在父进程中有这个:

import cp = require('child_process');
const k = cp.fork(file);

 app.use(function(req,res,next){       
      k.send('handle', req.socket);       
 };

然后我在子流程中有这个:

and then I have this in the child process:

process.on('message', function (m, socket) {

  if (m === 'handle' && socket) {
    socket.end('foobar!!!');
  }
  else{
    console.log('nope');
  }

});

执行一些日志记录后,我知道 socket.end(' foob​​ar !!!';; 正在调用。但我在浏览器中看到此错误:

After doing some logging, I know that socket.end('foobar!!!'); is getting called. But I see this error in the browser:

任何人都知道如何使用普通套接字发送有效回复吗?

Anyone know how I can send a valid response with just a plain socket at my disposal?

我想我需要学习如何实际编写有效的HTTP响应,从未尝试过t之前。

推荐答案

执行此操作时:

socket.end('foobar!!!');

您只需发送 foobar !!! 这不是一个有效的http响应,这正是您的客户端错误消息告诉您的。

Your are just sending foobar!!! and that is not a valid http response which is exactly what your client error message is telling you.

因为您已将此套接字传递给另一个进程并且express尚未在原始进程中向该套接字写入任何内容,所以您需要在此处发送完整的有效http响应。

Because you've passed this socket to another process and express has not yet written anything to that socket in the original process, you need to send a whole valid http response here.

http响应如下所示:

An http response looks like this:

归功于这篇文章图片的

Credit to this article for the image.

所以,一个非常简单的http响应可能如下所示:

So, a very simple http response could look like this:

socket.end('HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\nHello');

这篇关于子进程写入套接字 - localhost发送了无效的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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