0

I'm trying to create a android app that uses websockets. I'm using Gottox socket.io as my java socket io client and node js as my server. However, when i run my code I get an exception saying io.Socket.socketIOException:Error while handshaking on my android app. I have posted my code below (both java and node js). What am i doing wrong?

Here's my java code (client)

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;
    }
}

Here's my node js server

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

Androidアプリをどこで実行していますか? 変えようとする

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 に答える