2

I'm trying to develop an android application which will connect to the node.js server. Here's error msg:

04-28 14:24:52.250: W/System.err(811): io.socket.SocketIOException: Error while handshaking
04-28 14:24:52.250: W/System.err(811):  at io.socket.IOConnection.handshake(IOConnection.java:322)
04-28 14:24:52.259: W/System.err(811):  at io.socket.IOConnection.access$600(IOConnection.java:39)
04-28 14:24:52.259: W/System.err(811):  at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)
04-28 14:24:52.259: W/System.err(811): Caused by: java.lang.ArrayIndexOutOfBoundsException
04-28 14:24:52.306: W/System.err(811):  at io.socket.IOConnection.handshake(IOConnection.java:318)
04-28 14:24:52.306: W/System.err(811):  ... 2 more

Server:

 var http = require('http');
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
    }).listen(8881, '192.168.1.107');
    console.log('Server running at http://192.168.1.107:8881/');

Client:

public class RestClientActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            SocketIO socket = new SocketIO("http://192.168.1.107:8881/");
            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();
        }
    }

Any ideas what I'm doing wrong? If I type http://192.168.1.107:8881/ in browser it will display me "Hello World" on the page but I can't connect using socket.io-java-client library.

4

1 回答 1

2

首先,如果您尝试为您的服务使用休息客户端,则socket.io 不必坚持使用 HTTP 服务器,您会没事的,在客户端使用 HTTPClient ..

假设您使用我也使用过的这个 android socket.io 客户端Java Socket.io 客户端

试试这个作为服务器端套接字 io 脚本,这是LearnBoost/socket.io的基本示例

var server = require('http').Server();
var io = require('socket.io')(server);
io.on('connection', function(socket){
  socket.on('message', function(data){
    console.log("Received message : "+data);
    socket.emit("message","echoing back : "+data);
  });
  socket.on('disconnect', function(){
    // client disconnected
   });
});
server.listen(8881,"192.168.1.107");
于 2013-04-28T17:11:30.090 回答