Javascript Node.js Net模块,从回调函数中获取数据
所以我试着自己解决这个问题,用谷歌搜索了很多次,但我没能真正解决这个问题,尽管我认为这应该没那么难。因此,我正在做(或试图做)的是,我建立一个到服务器的简单telnet连接,并模拟SMTP会话的开始,以验证电子邮件是否存在(这是否给了我正确的结果是另一个问题) 我的代码如下所示:Javascript Node.js Net模块,从回调函数中获取数据,javascript,node.js,asynchronous,callback,Javascript,Node.js,Asynchronous,Callback,所以我试着自己解决这个问题,用谷歌搜索了很多次,但我没能真正解决这个问题,尽管我认为这应该没那么难。因此,我正在做(或试图做)的是,我建立一个到服务器的简单telnet连接,并模拟SMTP会话的开始,以验证电子邮件是否存在(这是否给了我正确的结果是另一个问题) 我的代码如下所示: const net = require('net'); async function TCPConnection(smtpServer, emailDomain, senderEmail) { const
const net = require('net');
async function TCPConnection(smtpServer, emailDomain, senderEmail) {
const client = new net.Socket();
client.setEncoding("utf-8");
let completeData = '';
client.connect({port: 25, host: smtpServer}, () => {
console.log("TCP Connection established with the Server.");
})
client.write("ehlo " + emailDomain + "\r\n");
client.write("mail from:<" + senderEmail + ">\r\n");
client.write("rcpt to:<" + senderEmail + ">\r\n");
client.on('data', function (data) {
console.log(data);
});
function dataReceived(data){
completeData += data;
}
client.on('end', function () {
console.log('Requested an end to the TCP connection');
});
}
TCPConnection('aspmx.l.google.com', 'mailtrap.io', 'new-recipient@mailtrap.io');
TCP Connection established with the Server.
220 mx.google.com ESMTP m17si1129948edf.309 - gsmtp
250-mx.google.com at your service, [217.84.87.66]
250-SIZE 157286400
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
250 2.1.0 OK m17si1129948edf.309 - gsmtp
550-5.1.1 The email account that you tried to reach does not exist. Please try
550-5.1.1 double-checking the recipient's email address for typos or
550-5.1.1 unnecessary spaces. Learn more at
550 5.1.1 https://support.google.com/mail/?p=NoSuchUser m17si1129948edf.309 - gsmtp
client.write("rcpt to:<" + senderEmail + ">\r\n","utf-8",()=>{
console.log("third command written");
client.end();
});
client.on('data', function (data) {
counterResponses++;
console.log("Response Nr: " + counterResponses + '\r\n' + data);
completeData.push(data);
if (counterResponses === 4){
console.log("4 responses received end connection");
client.end();
}
});
Response Nr: 1
220 mtaproxy104.free.mail.ir2.yahoo.com ESMTP ready
Response Nr: 2
250-mtaproxy104.free.mail.ir2.yahoo.com
250-PIPELINING
250-SIZE 41943040
250-8BITMIME
250 STARTTLS
Response Nr: 1
220 mtaproxy104.free.mail.ir2.yahoo.com ESMTP ready
250-mtaproxy104.free.mail.ir2.yahoo.com
250-PIPELINING
250-SIZE 41943040
250-8BITMIME
250 STARTTLS
因此,我想要实现的是从回调函数或TCPConnection函数中获取完整的数据(现在记录到控制台)。例如,在TCPConnection函数的末尾使用“return completeData;”
我想这样做的原因是,通过HTTP请求调用此函数(TCPConnection)。我想把结果发送给客户。完整的函数可能是这样的(但显然到目前为止还不起作用):
我阅读了解决方案,可以在“end”事件发出后对数据执行我想执行的操作。但我的问题是,在我的三个“写命令”之后,服务器不会立即结束连接。如果我写这封信:
client.write("ehlo " + emailDomain + "\r\n");
client.write("mail from:<" + senderEmail + ">\r\n");
client.write("rcpt to:<" + senderEmail + ">\r\n");
client.end();
client.write(“ehlo”+emailDomain+”\r\n”);
client.write(“邮件发件人:\r\n”);
client.write(“rcpt to:\r\n”);
client.end();
它在第二个和第三个命令得到响应之前结束连接。我试着用“等待客户。写…”来“等待”回复,但这显然行不通,因为我不知道是什么原因
如果你不想随便问的话,我希望你能知道我想做什么的要点。谢谢大家! 所以我认为我自己已经做到了,或者至少得到了一个在大多数情况下都能正常工作的解决方案。我所做的是在第三个write命令的回调中结束套接字,如下所示:
const net = require('net');
async function TCPConnection(smtpServer, emailDomain, senderEmail) {
const client = new net.Socket();
client.setEncoding("utf-8");
let completeData = '';
client.connect({port: 25, host: smtpServer}, () => {
console.log("TCP Connection established with the Server.");
})
client.write("ehlo " + emailDomain + "\r\n");
client.write("mail from:<" + senderEmail + ">\r\n");
client.write("rcpt to:<" + senderEmail + ">\r\n");
client.on('data', function (data) {
console.log(data);
});
function dataReceived(data){
completeData += data;
}
client.on('end', function () {
console.log('Requested an end to the TCP connection');
});
}
TCPConnection('aspmx.l.google.com', 'mailtrap.io', 'new-recipient@mailtrap.io');
TCP Connection established with the Server.
220 mx.google.com ESMTP m17si1129948edf.309 - gsmtp
250-mx.google.com at your service, [217.84.87.66]
250-SIZE 157286400
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
250 2.1.0 OK m17si1129948edf.309 - gsmtp
550-5.1.1 The email account that you tried to reach does not exist. Please try
550-5.1.1 double-checking the recipient's email address for typos or
550-5.1.1 unnecessary spaces. Learn more at
550 5.1.1 https://support.google.com/mail/?p=NoSuchUser m17si1129948edf.309 - gsmtp
client.write("rcpt to:<" + senderEmail + ">\r\n","utf-8",()=>{
console.log("third command written");
client.end();
});
client.on('data', function (data) {
counterResponses++;
console.log("Response Nr: " + counterResponses + '\r\n' + data);
completeData.push(data);
if (counterResponses === 4){
console.log("4 responses received end connection");
client.end();
}
});
Response Nr: 1
220 mtaproxy104.free.mail.ir2.yahoo.com ESMTP ready
Response Nr: 2
250-mtaproxy104.free.mail.ir2.yahoo.com
250-PIPELINING
250-SIZE 41943040
250-8BITMIME
250 STARTTLS
Response Nr: 1
220 mtaproxy104.free.mail.ir2.yahoo.com ESMTP ready
250-mtaproxy104.free.mail.ir2.yahoo.com
250-PIPELINING
250-SIZE 41943040
250-8BITMIME
250 STARTTLS
因为我想在收到4个命令响应后结束套接字,所以我使用if语句来检查响应的数量。但问题是,有时多个响应可以作为一个响应发送。例如,响应可能如下所示:
const net = require('net');
async function TCPConnection(smtpServer, emailDomain, senderEmail) {
const client = new net.Socket();
client.setEncoding("utf-8");
let completeData = '';
client.connect({port: 25, host: smtpServer}, () => {
console.log("TCP Connection established with the Server.");
})
client.write("ehlo " + emailDomain + "\r\n");
client.write("mail from:<" + senderEmail + ">\r\n");
client.write("rcpt to:<" + senderEmail + ">\r\n");
client.on('data', function (data) {
console.log(data);
});
function dataReceived(data){
completeData += data;
}
client.on('end', function () {
console.log('Requested an end to the TCP connection');
});
}
TCPConnection('aspmx.l.google.com', 'mailtrap.io', 'new-recipient@mailtrap.io');
TCP Connection established with the Server.
220 mx.google.com ESMTP m17si1129948edf.309 - gsmtp
250-mx.google.com at your service, [217.84.87.66]
250-SIZE 157286400
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
250 2.1.0 OK m17si1129948edf.309 - gsmtp
550-5.1.1 The email account that you tried to reach does not exist. Please try
550-5.1.1 double-checking the recipient's email address for typos or
550-5.1.1 unnecessary spaces. Learn more at
550 5.1.1 https://support.google.com/mail/?p=NoSuchUser m17si1129948edf.309 - gsmtp
client.write("rcpt to:<" + senderEmail + ">\r\n","utf-8",()=>{
console.log("third command written");
client.end();
});
client.on('data', function (data) {
counterResponses++;
console.log("Response Nr: " + counterResponses + '\r\n' + data);
completeData.push(data);
if (counterResponses === 4){
console.log("4 responses received end connection");
client.end();
}
});
Response Nr: 1
220 mtaproxy104.free.mail.ir2.yahoo.com ESMTP ready
Response Nr: 2
250-mtaproxy104.free.mail.ir2.yahoo.com
250-PIPELINING
250-SIZE 41943040
250-8BITMIME
250 STARTTLS
Response Nr: 1
220 mtaproxy104.free.mail.ir2.yahoo.com ESMTP ready
250-mtaproxy104.free.mail.ir2.yahoo.com
250-PIPELINING
250-SIZE 41943040
250-8BITMIME
250 STARTTLS
但也像这样:
const net = require('net');
async function TCPConnection(smtpServer, emailDomain, senderEmail) {
const client = new net.Socket();
client.setEncoding("utf-8");
let completeData = '';
client.connect({port: 25, host: smtpServer}, () => {
console.log("TCP Connection established with the Server.");
})
client.write("ehlo " + emailDomain + "\r\n");
client.write("mail from:<" + senderEmail + ">\r\n");
client.write("rcpt to:<" + senderEmail + ">\r\n");
client.on('data', function (data) {
console.log(data);
});
function dataReceived(data){
completeData += data;
}
client.on('end', function () {
console.log('Requested an end to the TCP connection');
});
}
TCPConnection('aspmx.l.google.com', 'mailtrap.io', 'new-recipient@mailtrap.io');
TCP Connection established with the Server.
220 mx.google.com ESMTP m17si1129948edf.309 - gsmtp
250-mx.google.com at your service, [217.84.87.66]
250-SIZE 157286400
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
250 2.1.0 OK m17si1129948edf.309 - gsmtp
550-5.1.1 The email account that you tried to reach does not exist. Please try
550-5.1.1 double-checking the recipient's email address for typos or
550-5.1.1 unnecessary spaces. Learn more at
550 5.1.1 https://support.google.com/mail/?p=NoSuchUser m17si1129948edf.309 - gsmtp
client.write("rcpt to:<" + senderEmail + ">\r\n","utf-8",()=>{
console.log("third command written");
client.end();
});
client.on('data', function (data) {
counterResponses++;
console.log("Response Nr: " + counterResponses + '\r\n' + data);
completeData.push(data);
if (counterResponses === 4){
console.log("4 responses received end connection");
client.end();
}
});
Response Nr: 1
220 mtaproxy104.free.mail.ir2.yahoo.com ESMTP ready
Response Nr: 2
250-mtaproxy104.free.mail.ir2.yahoo.com
250-PIPELINING
250-SIZE 41943040
250-8BITMIME
250 STARTTLS
Response Nr: 1
220 mtaproxy104.free.mail.ir2.yahoo.com ESMTP ready
250-mtaproxy104.free.mail.ir2.yahoo.com
250-PIPELINING
250-SIZE 41943040
250-8BITMIME
250 STARTTLS
因此,2个响应作为一个响应发送。这使得我很难评估自己是否得到了所有需要得到的回答。这就是为什么我在最后一个写命令之后才结束套接字
在client.on('end')函数中,我将完成的数据发送回客户端
client.on('end', function () {
console.log('Requested an end to the TCP connection');
console.log(completeData);
res.status(200).json(completeData);
});
唯一仍然困扰我的是我的数组(我用每个发出的“数据”事件推送数据)的长度是可变的。这会让确认电子邮件是否被找到变得有点困难。但是,由于这应该始终是最后一个响应,因此如果我只测试数组的最后一个条目中是否包含状态代码“250”或“550”,那么在大多数情况下,它都会起作用
我希望这至少对某些人有所帮助。让我知道它是否对你有帮助,或者如果你对此有任何疑问。同时,由于我确信有更好的方法可以做到这一点,所以仍然可以寻求更好的解决方案