0

我正在尝试创建一个使用 websockets 的 android 应用程序。我使用 Gottox socket.io 作为我的 java socket io 客户端和 node js 作为我的服务器。但是,当我运行我的代码时,我得到一个异常,说io.Socket.socketIOException:在我的 android 应用程序上握手时出错。我已经在下面发布了我的代码(java 和 node js)。我究竟做错了什么?

这是我的java代码(客户端)

package terrible.game.tiktakterrible;

import org.json.JSONException;
import org.json.JSONObject;

import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIO;
import io.socket.SocketIOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class TikTakTerrible extends Activity {

    TextView textView;
    SocketIO socket;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tik_tak_terrible);

        textView = (TextView) findViewById(R.id.textView);

        try {
            connect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void connect() throws Exception
    {
        socket = new SocketIO("http://localhost:5000/");

        socket.connect(new IOCallback() {
            @Override
            public void onMessage(JSONObject json, IOAcknowledge ack) {
                try {
                    setText("Server said:" + json.toString(2));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

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

            @Override
            public void onError(SocketIOException socketIOException) {
                setText(socketIOException.toString());
                socketIOException.printStackTrace();
            }

            @Override
            public void onDisconnect() {
                setText("Connection terminated.");
            }

            @Override
            public void onConnect() {
                setText("Connection established");
            }

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

        socket.send("Hello Server!");
    }

    public void setText(String arg)
    {
        textView.setText(arg);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.tik_tak_terrible, menu);
        return true;
    }
}

这是我的节点 js 服务器

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

app.listen(5000);

function handler(req, res)
{
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}

io.sockets.on('connection', function (socket) {

});
4

2 回答 2

0

你在哪里运行你的安卓应用程序?尝试改变

socket = new SocketIO("http://localhost:5000/");

socket = new SocketIO("http://your_pc_ip:5000/");

我认为这可能是问题所在。

于 2013-04-15T16:23:29.320 回答
0
            public void on(String event, IOAcknowledge ack, Object... args)
            {
             System.out.println( event );
                 if ("sensor".equals(event) && args.length > 0) 
                 {
                    try
                    {
                        JSONObject ob = new JSONObject(args[0].toString());
                        final  String mensaje = "Sensor: " + ob.getString("sensor") + " Valor: "+  ob.getString("valor");

                            new Thread(new Runnable()
                            {
                                public void run() 
                                {

                                    handler.post(new Runnable() {
                                        public void run() 
                                        {
                                            if(mensaje != null) 
                                            {
                                                txt.setText(mensaje);
                                            }
                                        }
                                    });
                                }
                            }).start();

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
于 2014-06-11T16:37:00.287 回答