[英]Android - receive messages from server using socket io
I am developing an android application using Socket.IO-Java-Client by Gottox (AndroidAsync by koush as well). 我正在使用Gottox的Socket.IO-Java-Client开发Android应用程序(同样由koush开发AndroidAsync)。 I have no problems connecting to the socket or sending messages over the socket, but I am not receiving messages or responses from the socket. 连接到套接字或通过套接字发送消息没有问题,但是我没有从套接字接收消息或响应。
The activity starts off receiving a list of messages through an API, and then opens a socket to send and receive outgoing/incoming messages. 该活动首先通过API接收消息列表,然后打开套接字以发送和接收传出/传入消息。 Any new messages appear if I restart the activity, but this is through the API - I need to receive them from the socket while the activity is still open. 如果我重新启动活动,则会出现任何新消息,但这是通过API进行的-我需要在活动仍处于打开状态时从套接字接收消息。
Here is my socket-related code: 这是我与套接字相关的代码:
public final void openSocket() {
try {
socket = new SocketIO(SERVER_URL);
socket.connect(this);
join();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public void join() {
try {
JSONObject json = new JSONObject();
json.putOpt("join", mChat.getID());
socket.emit("join", this, json);
// socket.emit("join", json);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void sendMessage() {
try {
System.out.println("Send: " + content.getTextWithEmojis());
JSONObject json = new JSONObject();
json = prepareJSONObject(json);
// socket.emit("send", this, json);
socket.emit("send", json);
content.setText("");
addChat(json);
} catch (JSONException ex) {
ex.printStackTrace();
}
}
public void addChat(JSONObject jObject) {
ChatPost post;
try {
post = parseJSONObject(jObject);
mChatMessages.add(Arrays.asList(new ChatPost[] {post}));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onMessage(JSONObject json, IOAcknowledge ack) {
try {
Log.i(TAG, "Server said:" + json.toString());
if(json.has("args")) {
JSONArray jArray = json.getJSONArray("args");
JSONObject jObject = jArray.getJSONObject(0);
ChatPost post;
post = parseJSONObject(jObject);
mChatMessages.add(Arrays.asList(new ChatPost[] {post}));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onMessage(String data, IOAcknowledge ack) {
Log.i(TAG, "Server said: " + data);
}
@Override
public void onError(SocketIOException socketIOException) {
Log.i(TAG, "an Error occured");
socketIOException.printStackTrace();
}
@Override
public void onDisconnect() {
Log.i(TAG, "Connection terminated.");
}
@Override
public void onConnect() {
Log.i(TAG, "Connection established");
}
@Override
public void on(String event, IOAcknowledge ack, Object... args) {
Log.i(TAG, "Server triggered event '" + event + "'");
if (args.length > 0)
Log.i(TAG, "Args '" + args[0].toString() + "'");
for (Object arg : args)
Log.i(TAG, "Args '" + arg.toString() + "'");
}
Log: 日志:
I/io.socket(9753): > 5:::{"args":[{"join":"101"}],"name":"join"}
I/io.socket(9753): < 1::
I/io.socket(9753): < 5:::{"name":"ready"}
I/io.socket(9753): < 2::
I/io.socket(9753): < 2::
I/io.socket(9753): > 2::
I/io.socket(9753): > 2::
I/io.socket(9753): > 5:::{"args":[{"content":"test","chat_id":"101","user_id":"5"}],"name":"send"}
I/io.socket(9753): < 2::
I/io.socket(9753): > 2::
I receive a response for "join", but not for "send." 我收到“加入”的回复,但没有收到“发送”的回复。 Am I missing something? 我想念什么吗?
EDIT: I tried using AndroidAsync by koush, but encountered the same problem. 编辑:我尝试通过koush使用AndroidAsync,但是遇到了同样的问题。
抱歉,原来是后端问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.