简体   繁体   中英

Socket.io 0.9 emit respond on Android, using Gottox/socket.io-java-client

If somebody can show me, what's wrong with my code? I really spend a lot of time in attempt to figure out this issue. The "chatinfo" event, which you can see in the Log is passing automatically, after successful connection to the server.

I use: Gottox/socket.io-java-client on Android device, http://socket.io/ 0.9.16 on the server, authorization is by passing token in header on the connection establishing.

Code:

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");
    }
} 

Code:

@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;

Log

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::

Finally my problem was not so really problem. I just miss to add one more parameter to my emit. So nobody catch the server's respond.

The only one thing was to add:

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

And pass the IOAcknowledge to the emit method:

socket.emit(EventList.GETLIST, ack, new JSONObject().put("sortByDate", true));

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM