0

如果有人可以告诉我,我的代码有什么问题?我真的花了很多时间试图弄清楚这个问题。您可以在日志中看到的“chatinfo”事件在成功连接到服务器后自动传递。

我使用: Android 设备上 的Gottox/socket.io-java-client ,服务器上的http://socket.io/ 0.9.16,授权是通过在连接建立时在标头中传递令牌。

代码:

private void getList(SocketIO socket) {
    try {
        socket.emit(EventList.GETLIST, new JSONObject().put("sortByDate", true));
        Log.v("getList", "emitted");
    } catch (JSONException e) {
        e.printStackTrace();
        Log.v("getList", "not emitted");
    }
} 

代码:

@Override
public void on(String s, IOAcknowledge ioAcknowledge, Object... objects) {
    JSONObject respond = (JSONObject) objects[0];
    Log.d("testFromOn", "String s = " + s);
    Log.d("testFromOn", "on" + respond);
    switch (s) {
        case GETLIST:
            Log.d("GETLIST", "GETLIST responce: " + s);
            break;

日志

I/io.socket﹕ < 1::
D/test﹕ onConnect
I/io.socket﹕ < 5:::{"name":"chatinfo","args":[{"onlineCount":0,"onlineFriendsCount":0,"unreadCount":7,"unreadFriendsCount":0,"userInfo":{"avatar":"1415903272343.jpg","url":"/user/5431b4955518085d5db4be23","_id":"5431b4955518085d5db4be23"}}]}
D/testFromOn﹕ String s = chatinfo
D/testFromOn﹕ on{"onlineCount":0,"onlineFriendsCount":0,"unreadCount":7,"unreadFriendsCount":0,"userInfo":{"avatar":"1415903272343.jpg","url":"\/user\/5431b4955518085d5db4be23","_id":"5431b4955518085d5db4be23"}}
I/io.socket﹕ > 5:::{"name":"getlist","args":[{"sortByDate":true}]}
V/getList﹕ emitted
I/io.socket﹕ > 2::
I/io.socket﹕ > 5:::{"name":"getlist","args":[{"sortByDate":true}]}
V/getList﹕ emitted
I/io.socket﹕ < 2::
I/io.socket﹕ > 2::
4

1 回答 1

1

最后我的问题不是那么真正的问题。我只是想在我的发射中再添加一个参数。所以没有人抓住服务器的响应。

唯一的一件事是添加:

IOAcknowledge ack = new IOAcknowledge() {
@Override
public void ack(Object... args) {
    if (args.length > 0) {
        Log.d("SocketIO", "" + args[0]);
    }
}
}

并将 IOAcknowledge 传递给 emit 方法:

socket.emit(EventList.GETLIST, ack, new JSONObject().put("sortByDate", true));
于 2015-02-11T06:53:59.860 回答