2013-10-29 150 views
1

我正在開發使用Gottox的Socket.IO-Java-Client(由koush提供的AndroidAsync)的android應用程序。我沒有問題連接到套接字或通過套接字發送消息,但我沒有收到來自套接字的消息或響應。Android - 使用套接字接收來自服務器的消息io

活動開始通過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,但遇到同樣的問題。

回答

相關問題