2016-07-25 60 views
0

我需要連接到socket併發送憑據信息才能通過接收即將發生的數據RECEIVE_CRISIS在Android中通過套接字發送數據

我正在使用庫適用於Android的Socket.IO。由於我沒有從連接中獲得日誌,所以調試真的很難,我不知道爲什麼以及它在哪裏失敗。自從我開始在Android上工作以來,我從未從服務器收到任何內容。 Android上的NSDictionary是否等於JSONObject?在Android上,sendEvent等於send()還是emit()?我是否需要將JSON作爲對象或數組發送?最後,如何獲取日誌錯誤?

它的工作iOS上的一部分,所以我有點失去..

這是的iOS代碼:(我修改地址爲安全目的):

NSDictionary *[email protected]{@"user":_username,@"orgid":_organizationId}; 
[_socketIO connectToHost:@"nodejs.myserver.com" onPort:3000 ]; 
    [_socketIO sendEvent:@"joinparty" withData:params]; 

這是安卓代碼:

private void connectAndListen(int username) throws Exception { 
    socket = IO.socket("nodejs.myserver.com:3000"); 
    socket.connect(); 

    JSONObject json = new JSONObject(); 
    json.put("user", username); 
    json.put("orgid", settings.getString("orgid", "")); 

    Log.e(TAG, json.toString()); 

    socket.send("joinparty",json); 
    socket.emit("joinparty", json); 

    socket.on(RECEIVE_CRISIS, onCrisis); 
} 

修訂問題

private void connectAndListen(int id) throws Exception { 
    IO.setDefaultSSLContext(SSLContext.getDefault()); 

    // Set default hostname 
    HostnameVerifier hostnameVerifier = new HostnameVerifier() { 
     @Override 
     public boolean verify(String hostname, SSLSession session) { 
      HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier(); 
      return true; 
     } 
    }; 
    IO.setDefaultHostnameVerifier(hostnameVerifier); 
    IO.Options options = new IO.Options(); 
    // set as an option 
    options.sslContext = SSLContext.getDefault(); 
    options.hostnameVerifier = hostnameVerifier; 
    options.secure = true; 
    socket = IO.socket("nodejs.myserver.com:3000", options); 
    socket.connect(); 
    JSONObject json = new JSONObject(); 
    json.put("user", id); 
    json.put("orgid", settings.getString("orgid", "")); 

    Map<Object, Object> arrays = new HashMap<>(); 
    arrays.put("user", id); 
    arrays.put("orgid",settings.getString("orgid", "")); 

    socket.emit("joinparty", arrays); 
    socket.on(RECEIVE_CRISIS, onCallback); 
    socket.on("connect_error", onCallback); 
    socket.on("connect", onCallback); 
    socket.on("Object", onCallback); 
    socket.on("connect_timeout", onCallback); 
} 

private Emitter.Listener onCallback = new Emitter.Listener() { 
    @Override 
    public void call(Object... args) { 
     Log.e(TAG, args[0]+""); 
    } 
}; 

回答

1

Is the NSDictionary is equivalent to the JSONObject on Android?

不,不是這樣的。 NSDictionary是class cluster,這意味着實際的實現對你來說是API用戶隱藏的。事實上,Foundation框架會在運行時選擇合適的實現基於數據等最接近它的Java量Map<Object, Object>

Is sendEvent equivalent to send() or emit() on Android?

它發出的Android。 from official documentation

DO I need to send the JSON as an Object or an Array?

嘗試發送Map來代替。 JSON是另一回事。如果您的服務器期望使用JSON格式的數據,則只需發送它。

Finally, How to I get the log error?

您需要爲它使用combination of emitter and manager

+0

現在我終於看到一個錯誤:xhr poll poll error。我認爲這可能與SSL認證有關,因爲我使用Retrofit這樣的問題。我更新了我的問題 – Jaythaking

+0

對於不同的錯誤,請發佈其他問題並提供必要的詳細信息。 –

+0

雖然最初的問題並未解決 – Jaythaking