在android中使用socket io java客户端的套接字io客户端

时间:2014-01-05 14:15:15

标签: java android node.js socket.io

我有一个节点服务器在运行。我想用socket连接到服务器。我正在使用socket io java客户端(https://github.com/Gottox/socket.io-java-client/wiki) 这是我的服务器代码:

var app = require('express')()
 , http = require('http')
 , server = http.createServer(app)
 , io = require('socket.io').listen(server);
io.sockets.on('connection', function(client)
{
console.log("client connected!");
client.emit('messages',{text: 'Hello world!'});
client.on('messages', function(data)
{
    console.log(data);
});
client.on('echo', function(data)
{
    console.log(data);
});
});
server.listen(8080);

这是我的安卓代码:

try {
        SocketIO socket = new SocketIO("http://10.0.2.2:9000/");
        socket.connect(new IOCallback() {

            @Override
            public void onMessage(JSONObject arg0, IOAcknowledge arg1) {
                try {
                    Toast.makeText(MainActivity.this,
                            arg0.getString("text"), Toast.LENGTH_LONG)
                            .show();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

            @Override
            public void onMessage(String arg0, IOAcknowledge arg1) {
                Toast.makeText(MainActivity.this, arg0, Toast.LENGTH_LONG)
                        .show();

            }

            @Override
            public void onError(SocketIOException arg0) {
                Log.d("tag", arg0.toString());

            }

            @Override
            public void onDisconnect() {
                // TODO Auto-generated method stub

            }

            @Override
            public void onConnect() {
                Toast.makeText(MainActivity.this, "Connected",
                        Toast.LENGTH_LONG).show();

            }

            @Override
            public void on(String event, IOAcknowledge ack, Object... args) {
                if ("echo back".equals(event) && args.length > 0) {
                    Log.d("SocketIO", "" + args[0]);
                    // -> "hello"
                }
            }
        });
        socket.emit("echo", "hello");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我的插座无法连接!我的意思是没有在Android中显示的吐司。这是我的服务器日志:

 debug - client authorized
   info  - handshake authorized -M9Dcv2a1Gd-LJpkogRF
   debug - setting request GET /socket.io/1/websocket/-M9Dcv2a1Gd-LJpkogRF
   debug - set heartbeat interval for client -M9Dcv2a1Gd-LJpkogRF
   debug - client authorized for 
   debug - websocket writing 1::
client connected!
   debug - websocket writing 5:::{"name":"messages","args":[{"text":"Hello world!"}]}
hello
   info  - transport end (undefined)
   debug - set close timeout for client -M9Dcv2a1Gd-LJpkogRF
   debug - cleared close timeout for client -M9Dcv2a1Gd-LJpkogRF
   debug - cleared heartbeat interval for client -M9Dcv2a1Gd-LJpkogRF
   debug - discarding transport

我的错在哪里?

1 个答案:

答案 0 :(得分:0)

可能是因为您已将Toast.makeText()放在错误的位置。当服务器执行onMessage()时,客户端上的socket.send()事件会触发,socket.emit()主要用于在客户端/服务器之间发送纯文本消息。

当服务器执行on(String event, IOAcknowledge ack, Object.. args)时,客户端会将其侦听到Toast.makeText()方法。因此,将on()替换为 @Override public void on(String event, IOAcknowledge ack, Object... args) { Object[] arguments = args; String json = arguments[0].toString(); if(event.equalsIgnoreCase("messages")){ // do something with json. } } 方法。

{{1}}