У меня есть небольшой тестовый клиент telnet, который должен выполнить авторизацию на устройстве Android.Он работает нормально, однако хотелось бы понять, если подход кажется правильным и не приведет к утечкам памяти.
Причина, по которой я чувствую, что этот скрипт может вызвать утечку памяти, заключается в том, что, когда соединение установлено на пенсии, я вижу несколькоподтверждение соединения:
node test.js
Connection refused, device not up yet..
Retrying..
Connection closed
CONNECTED TO: 127.0.0.1:5554
CONNECTED TO: 127.0.0.1:5554
Received: Android Console: Authentication required
Android Console: type 'auth <auth_token>' to authenticate
Android Console: you can find your <auth_token> in
'/Users/testr/.emulator_console_auth_token'
OK
Я ожидаю увидеть только один экземпляр CONNECTED TO: 127.0.0.1:5554
Я полагаю, что я где-то совершаю ошибку, чтобы закрыть старое соединение, но не могу понять, где.
Если сервер работает:
с первой попытки, аутентификация работает нормально:
CONNECTED TO: 127.0.0.1:5554
Received: Android Console: Authentication required
Android Console: type 'auth <auth_token>' to authenticate
Android Console: you can find your <auth_token> in
'/Users/testr/.emulator_console_auth_token'
OK
При повторной попытке подключения:
Connection refused, device not up yet..
Retrying..
Connection closed
CONNECTED TO: 127.0.0.1:5554
CONNECTED TO: 127.0.0.1:5554
Received: Android Console: Authentication required
Android Console: type 'auth <auth_token>' to authenticate
Android Console: you can find your <auth_token> in
'/Users/testr/.emulator_console_auth_token'
OK
const net = require('net');
const HOST = '127.0.0.1';
const Port = 5554;
let client = new net.Socket();
// connection
const conn = function Connect(Port) {
client.connect(Port, '127.0.0.1', function () {
console.log('CONNECTED TO: ' + '127.0.0.1' + ':' + Port);
client.write('auth testcred');
});
};
// error handling
client.on('error', function (error) {
if (error.code === 'ECONNREFUSED') {
console.log("Connection refused, device not up yet..");
console.log("Retrying..");
setTimeout(function() {
conn(Port);
}, 10000);
}
});
// on response from server
client.on('data', function(data) {
console.log('Received: ' + data);
client.destroy();
client.removeAllListeners();
});
// on connection closure
client.on('close', function() {
console.log('Connection closed');
client.destroy();
});
conn(Port);
Я ожидаю, что вывод просто вернет CONNECTED TO: 127.0.0.1:5554
только один раз, но я вижу, что он печатается равным количеству попыток.