如果有人可以告诉我,我的代码有什么问题?我真的花了很多时间试图弄清楚这个问题。您可以在日志中看到的“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::