Android developpement,Gottox socket.io-java-client:file not fount Exception /socket.io/1/

时间:2014-06-09 02:31:32

标签: java android exception socket.io handshake

这是我的问题:

我一直在尝试将socket.io-java-client用于Android应用,但我最终会出现握手错误(见下文)。

这是我的应用源代码:

public void runIO(){
    try {
        SocketIO socket = new SocketIO("http://192.168.1.60:1337");
        socket.connect(new IOCallback() {
            @Override
            public void onMessage(JSONObject json, IOAcknowledge ack) {
                try {
                    System.out.println("Server said:" + json.toString(2));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onMessage(String data, IOAcknowledge ack) {
                System.out.println("Server said: " + data);
            }

            @Override
            public void onError(SocketIOException socketIOException) {
                System.out.println("an Error occured");
                socketIOException.printStackTrace();
            }

            @Override
            public void onDisconnect() {
                System.out.println("Connection terminated.");
            }

            @Override
            public void onConnect() {
                System.out.println("Connection established");
            }

            @Override
            public void on(String event, IOAcknowledge ack, Object... args) {
                System.out.println("Server triggered event '" + event + "'");
            }
        });

        // This line is cached until the connection is establisched.
        socket.send("Hello Server!");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

和我的Node.js / Socket.io:

    var app = require('http').createServer(handler)
    var io = require('socket.io')(app); 
    var fs = require('fs');

    app.listen(1337);

    function handler (req, res) {
      fs.readFile(__dirname + '/index.html',
      function (err, data) {
        if (err) {
          res.writeHead(500);
          return res.end('Error loading index.html');
        }

        res.writeHead(200);
        res.end(data);
      });
    }

    io.on('connection', function (socket) {
      socket.emit('news', { hello: 'world' });
      socket.on('my other event', function (data) {
        console.log(data);
      });
    });

我收到以下错误:

    06-09 03:59:53.955: W/System.err(11738): io.socket.SocketIOException: Error while handshaking
    06-09 03:59:53.965: W/System.err(11738):    at io.socket.IOConnection.handshake(IOConnection.java:322)
    06-09 03:59:53.965: W/System.err(11738):    at io.socket.IOConnection.access$7(IOConnection.java:292)
    06-09 03:59:53.970: W/System.err(11738):    at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)
    06-09 03:59:53.970: W/System.err(11738): Caused by: java.io.FileNotFoundException: http://192.168.1.60:1337/socket.io/1/
    06-09 03:59:53.975: W/System.err(11738):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
    06-09 03:59:53.975: W/System.err(11738):    at io.socket.IOConnection.handshake(IOConnection.java:313)

在浏览器上运行,http://192.168.1.60:1337/socket.io/1/的页面返回以下消息(通过浏览器):

{"code":0,"message":"Transport unknown"}

我不知道如何使其发挥作用...请帮助:x

谢谢:D

编辑:

正如 Larky 所说,我不得不将我的Socket.io版本降级为 0.9.16 。它就像一个带有以下节点代码的魅力:

    var io = require('socket.io').listen(80); 

    io.on('connection', function (socket) {
      socket.emit('news', { hello: 'world' });
      socket.on('my other event', function (data) {
        console.log(data);
      });
    });

1 个答案:

答案 0 :(得分:7)

我今天通过在服务器端(早期版本的socket.io Node模块)恢复到早期版本的socket.io来解决了这个问题。协议可能改变了吗?

要做到这一点:

  1. 删除node_modules文件夹中的socket.io文件夹。

  2. 在控制台上输入:

  3.   

    npm install socket.io@0.9.16

    应该这样做......它对我有用:)

    (显然这并没有完全解决问题但是早期版本的socket.io对我而言对我来说没有问题)