简体   繁体   English

如何在套接字io和android聊天应用程序中检查用户是否在线

[英]How to check a user online or not in socket io and android chat application

I am using socket io and android for using chat application. 我正在使用套接字io和android使用聊天应用程序。 The server side is nodejs and client side is android. 服务器端是nodejs,客户端端是android。 My chat is working as fine but i want to check whether a user is active or not. 我的聊天工作正常,但我想检查用户是否处于活动状态。 For example i want to check wheather user "abc" is active or not. 例如,我想检查小麦用户“ abc”是否处于活动状态。

In Android part. 在Android部分。 I have get the reference. 我有参考。

emit("login",userid);

is used for get online users. 用于获取在线用户。 So i use 所以我用

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

The server code is shown below 服务器代码如下所示

var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var port = process.env.PORT || 86; 
server.listen(port, function () {
    console.log('Updated : Server listening at port %d', port);
});
app.use('/js',  express.static(__dirname + '/public/js'));
app.use('/css', express.static(__dirname + '/public/css'));
app.use(express.static(__dirname + '/public'));
var usernames = {};
var numUsers = 0; 
io.on('connection', function (socket) {
var addedUser = false;
socket.on('new message', function (data) {
  socket.broadcast.emit('new message', {
    username: socket.username,
    message: data,
    timestamp: Date.now()
  });
  console.log('I sent it');
});

// when the client emits 'add user', this listens and executes
socket.on('add user', function (username) {
  // we store the username in the socket session for this client
  socket.username = username;
  // add the client's username to the global list
  usernames[username] = username;
  ++numUsers;
  addedUser = true;
  socket.emit('login', {
    numUsers: numUsers
  });
  // echo globally (all clients) that a person has connected
  socket.broadcast.emit('user joined', {
    username: socket.username,
    numUsers: numUsers
  });
});

// when the client emits 'typing', we broadcast it to others
socket.on('typing', function () {
  socket.broadcast.emit('typing', {
    username: socket.username
  });
});

// when the client emits 'stop typing', we broadcast it to others
socket.on('stop typing', function () {
  socket.broadcast.emit('stop typing', {
    username: socket.username
  });
});

// when the user disconnects.. perform this
socket.on('disconnect', function () {
  // remove the username from global usernames list
  if (addedUser) {
    delete usernames[socket.username];
    --numUsers;

    // echo globally that this client has left
    socket.broadcast.emit('user left', {
      username: socket.username,
      numUsers: numUsers
    });
  }
});
});

How it possible please help me? 有什么可能请帮助我?

On the server side, add a new event listener to check if a specific user is active or not. 在服务器端,添加一个新的事件侦听器,以检查特定用户是否处于活动状态。

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

On the client side, emit an event asking for specific user active status. 在客户端,发出一个事件,询问特定的用户活动状态。

final JSONObject user = new JSONObject();
try {
  user.put("userId", "abc");
} catch (final JSONException e) {
  e.printStackTrace();
}
mSocket.emit("isActive", user);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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