【问题标题】:Audio transfer android and node-webkit音频传输 android 和 node-webkit
【发布时间】:2014-08-30 16:31:14
【问题描述】:

我一直在尝试在我的 android 应用程序和我的 node-webkit 应用程序之间传输音频文件,但我是 socket.io/nodejs/delivery.js 世界的新手。

这是我的代码:

android-code ERROR-LINE: 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-code

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.. 之间建立错误连接,我可能是错误的? 如果有人可以帮助我,我会很高兴。我已经环顾四周,但正如我所说,我是这个世界的新手,我变得朦胧 基本上我的问题是:怎么了?以及我如何实现我的目标?

感谢您的宝贵时间

【问题讨论】:

  • 使用这个而不是交付,它更好..github.com/vote539/socketio-file-upload
  • var io = require('socket.io').listen(80); var SocketIOFileUpload = require("socketio-file-upload"); io.on("连接", function(socket){ var uploader = new SocketIOFileUpload(); uploader.dir = "/musica"; uploader.listen(socket); });这是我尝试过的......仍然失败

标签: javascript android node.js sockets webkit


【解决方案1】:

我正在使用 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;

    });
});

【讨论】:

    猜你喜欢
    • 2015-02-18
    • 2013-05-31
    • 2014-10-14
    • 2012-11-06
    • 2014-11-04
    • 2016-05-12
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    相关资源
    最近更新 更多