我正在使用由Gottox。编写的Socket.IO-Client for Java制作android应用程序,我知道如何向服务器发送消息,但是我想知道如何从服务器接收消息或获取响应
这是我的代码示例

 SocketIO socket=new SocketIO();
        try {
            socket = new SocketIO("http://41.69.21.123:1333/");
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        socket.connect(new IOCallback() {
            public void onMessage(JSONObject json, IOAcknowledge ack) {
                try {
                    tv.setText(json.toString(2));
                    System.out.println("Server said:" + json.toString(2));


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

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

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

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

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

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


        socket.send("Hello Server!");


    }

最佳答案

我通过这种方法收到消息

  @Override
        public void on(String event, IOAcknowledge ack, Object... args) {
            System.out.println("Server triggered event '" + event + "'");
            //it comes with 3 parameters event, acknowledge and args
            //if you want to get args to String try this
              Object[] arguments = args;
            System.out.println("Args"+ arguments[0].toString());
        }


您可以在日志中使用socket.io标签检查服务器响应

关于android - 如何使用套接字io从服务器接收消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12356357/

10-11 01:33
查看更多