我正在使用Gottox的Socket.IO-Java-Client开发一个Android应用程序(koush的AndroidAsync)。我没有问题连接到套接字或通过套接字发送消息,但我没有从套接字接收消息或响应。
活动开始通过API接收消息列表,然后打开套接字以发送和接收传出/传入消息。如果我重新启动活动,则会显示任何新消息,但这是通过API - 我需要在活动仍处于打开状态时从套接字接收它们。
这是我的套接字相关代码:
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() + "'");
}
日志:
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::
我收到“加入”的回复,但不是“发送”。我错过了什么吗?
编辑:我尝试使用koush的AndroidAsync,但遇到了同样的问题。
答案 0 :(得分:0)
抱歉,原来是后端问题