使用 chrome.sockets.tcp 将 FTP 客户端构建为 Chrome 应用。
起初,每当我尝试发送密码时,我都会收到“500 无效命令:尝试更有创意”。然后我意识到,无论我发送什么命令,它都会在第二个命令上返回该错误。
即。 USER,然后是 PASS,会给我错误。 FEAT,然后是 USER,会给我错误。 FEAT,然后是 HELP,会给我错误。
我尝试在发出第二个命令之前等待一段时间。没有效果。
chrome.app.window.create('index.html', {
id: "helloWorldID",
bounds: {
width: width,
height: height,
left: Math.round((screenWidth-width)/2),
top: Math.round((screenHeight-height)/2)
}
});
var IP = "server.com";
var PORT = 21;
var ftp_username = "user";
var ftp_password = "password";
var socketID;
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
function str2ab(str) {
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
var bufView = new Uint8Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function onCallback( result ){
console.log( "onCallback", result );
};
chrome.sockets.tcp.create({ persistent:false }, function(createInfo) {
socketID = createInfo.socketId;
chrome.sockets.tcp.connect( socketID, IP, PORT, onCallback);
});
chrome.sockets.tcp.onReceive.addListener(function(info) {
//console.log( "onReceive", info.socketId );
var d = ab2str( info.data );
console.log( d );
if (d.indexOf("220 ")>-1){
// send user.
console.log( "send " + ftp_username);
chrome.sockets.tcp.send( socketID, str2ab("USER "+ftp_username+"\n"), onCallback );
}
if (d.indexOf("331 ")>-1){
// send password.
console.log( "send password");
chrome.sockets.tcp.send( socketID, str2ab("PASS "+ftp_password+"\n"), onCallback );
}
});
chrome.sockets.tcp.onReceiveError.addListener(function(info) {
console.log( "onReceiveError", info );
});
});
最佳答案
您必须在 ArrayBuffer 中为每个字符保留 1 个字节,而不是两个
var buf = new ArrayBuffer(str.length); // 1 byte for each char
关于sockets - 第二次 chrome.sockets.tcp.send 调用始终导致 "500 Invalid command: try being more creative",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24413362/