gpt4 book ai didi

android - 如何使用socket io从服务器接收消息

转载 作者:行者123 更新时间:2023-11-29 21:59:23 25 4
gpt4 key购买 nike

我正在使用由 Gottox 编写的用于 Java 的 Socket.IO-Client 制作一个 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 - 如何使用socket io从服务器接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12356357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com