我正在开发Android私人聊天应用程序。在这个应用程序中,我想看到在线用户或检查用户是否在线。我有参考。
emit("login",userid);
用于获取在线用户。所以我用
public class OnlineActivity extends AppCompatActivity {
private Socket mSocket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_online);
ChatApplication app = (ChatApplication) getApplication();
mSocket = app.getSocket();
JSONObject user = new JSONObject();
try {
user.put("userId", "abc");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mSocket.emit("login",user);
Log.d("LOG", String.valueOf(mSocket.emit("login",user)));
Log.d("LOG", String.valueOf(user));
}
}
其中abc是用户ID,但不显示任何结果。请帮帮我。
答案 0 :(得分:0)
答案显示在How to check a user online or not in socket io and android chat application
中socket.on('isActive', function (user) {
const userId = user.userId;
const user = usernames[userId];
const socketId = socket.id;
let response;
if(user) {
// User is active
response = {isActive: true};
} else {
// User is not active
response = {isActive: false};
}
const responseSocket = io.sockets.connected[socketId];
if(responseSocket) {
responseSocket.emit('onIsActive', response);
}
});