[英]How to check a user online or not in socket io and android chat application
我正在使用套接字io和android使用聊天應用程序。 服務器端是nodejs,客戶端端是android。 我的聊天工作正常,但我想檢查用戶是否處於活動狀態。 例如,我想檢查小麥用戶“ abc”是否處於活動狀態。
在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
});
}
});
});
有什么可能請幫助我?
在服務器端,添加一個新的事件偵聽器,以檢查特定用戶是否處於活動狀態。
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);
}
});
在客戶端,發出一個事件,詢問特定的用戶活動狀態。
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.