【问题标题】:Android socket io get online userAndroid socket io 获取在线用户
【发布时间】:2017-04-10 16:45:34
【问题描述】:

我正在开发一个 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,但它不显示任何结果。请帮帮我。

【问题讨论】:

    标签: java android websocket socket.io


    【解决方案1】:

    答案显示在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);
    }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多