我正在使用socket 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
});
}
});
});
怎么可能请帮帮我?
答案 0 :(得分:1)
在服务器端,添加一个新的事件监听器以检查特定用户是否处于活动状态。
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);