gpt4 book ai didi

java - 如何在socket io和android聊天应用程序中检查用户是否在线

转载 作者:行者123 更新时间:2023-12-02 13:28:07 25 4
gpt4 key购买 nike

我正在使用套接字 io 和 android 来使用聊天应用程序。服务器端是nodejs,客户端是android。我的聊天工作正常,但我想检查用户是否活跃。例如,我想检查用户“abc”是否处于 Activity 状态。

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

服务器代码如下所示

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

请帮助我怎么可能?

最佳答案

在服务器端,添加一个新的事件监听器来检查特定用户是否处于 Activity 状态。

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

在客户端,发出一个事件,询问特定的用户 Activity 状态。

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

关于java - 如何在socket io和android聊天应用程序中检查用户是否在线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43316913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com