I am developing an android application using Socket.IO-Java-Client by Gottox (AndroidAsync by koush as well). 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. 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.
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.
抱歉,原来是后端问题
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.