【发布时间】:2018-10-05 16:10:51
【问题描述】:
我有这个 Lambda 函数:
exports.handler = async (event,context,callback) => {
// console.log('Received event:', JSON.stringify(event, null, 2));
var key=event.queryStringParameters.ref;
// console.log('index.handler started with key : '+key);
var RemoteServer='aaa.bbb.ccc.ddd';
var net = require('net');
var responce={
"statusCode": 200,
"headers": {
"Content-Type": "*/*"
}
};
var socket = new net.Socket();
socket.setEncoding('utf8');
socket.on('close', () => {
console.log('Close.');
console.log(JSON.stringify(responce));
callback(null, JSON.stringify(responce));
});
socket.on('connect', () => {
// console.log('Connect');
var senddata='1,'+key;
// console.log(('About to write :'+senddata));
socket.write(senddata);
console.log(('Wrote :'+senddata));
});
socket.on('data', (data) => {
console.log('Data.');
responce.body = data;
console.log(JSON.stringify(responce));
socket.end();
});
socket.on('end', () => {
console.log('End.');
console.log(JSON.stringify(responce));
});
socket.on('error', (error) => {
console.log('Error' + error);
socket.destroy;
callback(error);
});
socket.on('timeout', () => {
console.log('Timeout');
socket.close;
callback('Timeout');
});
socket.connect(11010, RemoteServer, () => {
// console.log('socket connect');
});
callback(null,responce); // This is here to get this to do anything.
};
它的工作原理是,console.log 显示我在 socket.on('data') 例程中获得了正确的数据。
但是,它无法在回调中返回 response.body。 此外,它仅在我在异步函数中有回调时才有效,而不是在事件侦听器中。
这是我的输出 - console.log:
START RequestId: 7e1876fe-4255-12de-56d6-c187bcfff9ee Version: $LATEST
Wrote :1,R9H39X
Data.
{"statusCode":200,"headers":{"Content-Type":"*/*"},"body":"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<group>\r\n <data>\r\n <name>COLIN MANNING</name>\r\n <from>26/03/2018</from>\r\n <to>31/05/2018</to>\r\n <room>A31</room>\r\n <location>X,Y</location>\r\n </data>\r\n</group>\r\n"}
End.
{"statusCode":200,"headers":{"Content-Type":"*/*"},"body":"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<group>\r\n <data>\r\n <name>COLIN MANNING</name>\r\n <from>26/03/2018</from>\r\n <to>31/05/2018</to>\r\n <room>A31</room>\r\n <location>X,Y</location>\r\n </data>\r\n</group>\r\n"}
Close.
{"statusCode":200,"headers":{"Content-Type":"*/*"},"body":"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<group>\r\n <data>\r\n <name>COLIN MANNING</name>\r\n <from>26/03/2018</from>\r\n <to>31/05/2018</to>\r\n <room>A31</room>\r\n <location>X,Y</location>\r\n </data>\r\n</group>\r\n"}
END RequestId: 7e1876fe-4255-12de-56d6-c187bcfff9ee
REPORT RequestId: 7e1876fe-4255-12de-56d6-c187bcfff9ee Duration: 178.76 ms Billed Duration: 200 ms Memory Size: 1536 MB Max Memory Used: 33 MB
和回调:
{"statusCode":200,"headers":{"Content-Type":"*/*"}}
这是没有最后一个回调的 console.log:
START RequestId: c3dc7e69-87aa-1608-76d4-093a0e28711b Version: $LATEST
END RequestId: c3dc7e69-87aa-1608-76d4-093a0e28711b
REPORT RequestId: c3dc7e69-87aa-1608-76d4-093a0e28711b Duration: 3.43 ms Billed Duration: 100 ms Memory Size: 1536 MB Max Memory Used: 33 MB
和回调:
null
所以没有回调,什么都不会运行。
我想要实现的是: 打开到服务器的套接字。 通过套接字发送数据 通过套接字从服务器获取回复 关闭套接字 回复 API 网关
这是我第一次尝试 Node.js 异步、Lambda 和 API Gateway。 我认为我理解 Node.js 异步和回调的方式有误。
如何从 socket.on('close') 监听器返回数据?
在我看来,监听器中的回调从未被调用,我无法确定如何使异步函数中的回调等待来自监听器的数据。
我是否应该使用 EventAsPromise 然后阻塞一段时间 (!done) {....}?这似乎违背了 Node 异步理念。
【问题讨论】:
标签: node.js amazon-web-services sockets asynchronous callback