我一直在尝试在我的 Android 应用程序和 Node-Webkit 应用程序之间传输音频文件,但我对 socket.io/nodejs/delivery.js 的世界还不熟悉。
这是我的代码:
android 代码错误行:os.write(mybytearray, 0, mybytearray.length);
protected Void doInBackground(Void... arg0) {
Socket sock;
try {
// sock = new Socket("MY_PCs_IP", 1149);
sock = new Socket("192.168.0.10", 5001);
System.out.println("Connecting...");
// sendfile
File myFile = new File(this.currentSong.getPath());
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = sock.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray, 0, mybytearray.length);
os.flush();
System.out.println("Sended..");
// RESPONSE FROM THE SERVER
BufferedReader in = new BufferedReader(
new InputStreamReader(sock.getInputStream()));
in.ready();
String userInput = in.readLine();
System.out.println("Response from server..." + userInput);
sock.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
Node webkit 代码
var io = require('socket.io').listen(5001),
dl = require('delivery'), //delivery.server
fs = require('fs');
io.sockets.on('connection', function(socket){
delivery = dl.listen(socket);
delivery.on('receive.success',function(file){
fs.writeFile("music/"+file.name,file.buffer, function(err){
if(err){
console.log('File could not be saved.');
}else{
console.log('File saved.');
addSong("music/"+file.name);
};
});
});
});
注意:我的服务器端运行良好,已经通过 js 客户端进行了测试
这是我收到的错误:
Android端错误:
08-28 14:56:36.180: W/System.err(30510): java.net.SocketException: sendto failed: EPIPE (Broken pipe)
08-28 14:56:36.180: W/System.err(30510): at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:499)
08-28 14:56:36.180: W/System.err(30510): at libcore.io.IoBridge.sendto(IoBridge.java:468)
08-28 14:56:36.180: W/System.err(30510): at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)
所以也许我因为套接字和socket.io之间的协议(protocol)而试图建立一个错误的连接是错误的? 如果有人能帮助我,我会很高兴。我已经环顾四周,但正如我所说,我是这个世界的新手,我变得模糊 基本上我的问题是:出了什么问题?以及我如何实现我的目标?
感谢您的宝贵时间
最佳答案
我正在使用 com.koushikdutta.async.http.socketio.SocketIOClient 这个库和socket.io存在一些问题,但是通过使用对node-webkit的依赖关系解决了这个问题
"socket.io": "~0.9",
还需要读取文件->base64编码->然后在服务器端发出字符串必须这样做:
socket.on('finishFileTransfer',function(){
fs.writeFile("music/"+fileName,new Buffer(file,'base64'), function(err){
if(err){
console.log('File could not be saved.');
}else{
console.log('File saved.');
addSong("musica/"+fileName);
}
file = "";
fileName = null;
});
});
关于javascript - 音频传输android和node-webkit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25584565/