send online pings; update user states
This commit is contained in:
parent
f406548b4b
commit
5f43afd312
|
@ -34,6 +34,17 @@ class Users {
|
|||
}
|
||||
return userList;
|
||||
}
|
||||
|
||||
updateUsersState() {
|
||||
let currentTime = (new Date().getTime());
|
||||
for (let pubkey in this.users) {
|
||||
let user = this.users[pubkey];
|
||||
if (currentTime - user.lastSeen > 10*1000) {
|
||||
user.online = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ChannelManager {
|
||||
|
|
28
src/index.js
28
src/index.js
|
@ -34,10 +34,22 @@ var updateUsers = function() {
|
|||
ui.availableUsers(users)
|
||||
}
|
||||
|
||||
var handleProtocolMessages = function(channelName, data) {
|
||||
// TODO: yes this is ugly, can be moved to the lib level
|
||||
let msg = JSON.parse(JSON.parse(data.payload)[1][0]);
|
||||
let fromUser = data.data.sig;
|
||||
|
||||
if (msg.type === 'ping') {
|
||||
let user = channels.allUsers.addOrUpdateUserKey(fromUser, data.username);
|
||||
let channel = channels.getChannel(channelName);
|
||||
channel.users.addUserOrUpdate(user);
|
||||
channels.events.emit("update");
|
||||
}
|
||||
}
|
||||
|
||||
channels.events.on('update', updateUsers);
|
||||
channels.events.on('channelSwitch', updateUsers);
|
||||
|
||||
|
||||
ui.logEntry(`
|
||||
Welcome to
|
||||
_________ __ __ ____ ___
|
||||
|
@ -56,6 +68,12 @@ ui.logEntry(`-----------------------------------------------------------`)
|
|||
var status = new StatusJS();
|
||||
status.connect("ws://localhost:8546");
|
||||
|
||||
setInterval(function() {
|
||||
status.sendJsonMessage(channels.getCurrentChannel().name, {type: "ping"});
|
||||
|
||||
channels.allUsers.updateUsersState();
|
||||
}, 5 * 1000);
|
||||
|
||||
status.joinChat(DEFAULT_CHANNEL, () => {
|
||||
ui.logEntry(("Joined #" + DEFAULT_CHANNEL).green.underline)
|
||||
|
||||
|
@ -64,7 +82,11 @@ status.joinChat(DEFAULT_CHANNEL, () => {
|
|||
status.onMessage(DEFAULT_CHANNEL, (err, data) => {
|
||||
let msg = JSON.parse(data.payload)[1][0];
|
||||
|
||||
if (JSON.parse(data.payload)[1][1] === 'content/json') {
|
||||
handleProtocolMessages(DEFAULT_CHANNEL, data);
|
||||
} else {
|
||||
channels.addMessage(DEFAULT_CHANNEL, msg, data.data.sig, data.username)
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -80,7 +102,11 @@ ui.events.on('cmd', (cmd) => {
|
|||
status.onMessage(channelName, (err, data) => {
|
||||
let msg = JSON.parse(data.payload)[1][0];
|
||||
|
||||
if (JSON.parse(data.payload)[1][1] === 'content/json') {
|
||||
handleProtocolMessages(channelName, data);
|
||||
} else {
|
||||
channels.addMessage(channelName, msg, data.data.sig, data.username)
|
||||
}
|
||||
});
|
||||
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue