send online pings; update user states

This commit is contained in:
Iuri Matias 2018-11-07 13:19:27 -05:00
parent f406548b4b
commit 5f43afd312
3 changed files with 42 additions and 5 deletions

View File

@ -34,6 +34,17 @@ class Users {
} }
return userList; 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 { class ChannelManager {

View File

@ -16,7 +16,7 @@ channels.events.on('channelSwitch', () => {
ui.logEntry("-------------------"); ui.logEntry("-------------------");
ui.logEntry("now viewing #" + channels.getCurrentChannel().name); ui.logEntry("now viewing #" + channels.getCurrentChannel().name);
channels.dumpPendingMessages().forEach((message) => { channels.dumpPendingMessages().forEach((message) => {
let msg = (message.username + ">").green + " " + message.message; let msg = (message.username + ">").green + " " + message.message;
ui.logEntry(msg); ui.logEntry(msg);
}); });
}); });
@ -34,10 +34,22 @@ var updateUsers = function() {
ui.availableUsers(users) 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('update', updateUsers);
channels.events.on('channelSwitch', updateUsers); channels.events.on('channelSwitch', updateUsers);
ui.logEntry(` ui.logEntry(`
Welcome to Welcome to
_________ __ __ ____ ___ _________ __ __ ____ ___
@ -56,6 +68,12 @@ ui.logEntry(`-----------------------------------------------------------`)
var status = new StatusJS(); var status = new StatusJS();
status.connect("ws://localhost:8546"); status.connect("ws://localhost:8546");
setInterval(function() {
status.sendJsonMessage(channels.getCurrentChannel().name, {type: "ping"});
channels.allUsers.updateUsersState();
}, 5 * 1000);
status.joinChat(DEFAULT_CHANNEL, () => { status.joinChat(DEFAULT_CHANNEL, () => {
ui.logEntry(("Joined #" + DEFAULT_CHANNEL).green.underline) ui.logEntry(("Joined #" + DEFAULT_CHANNEL).green.underline)
@ -64,7 +82,11 @@ status.joinChat(DEFAULT_CHANNEL, () => {
status.onMessage(DEFAULT_CHANNEL, (err, data) => { status.onMessage(DEFAULT_CHANNEL, (err, data) => {
let msg = JSON.parse(data.payload)[1][0]; let msg = JSON.parse(data.payload)[1][0];
channels.addMessage(DEFAULT_CHANNEL, msg, data.data.sig, data.username) 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) => { status.onMessage(channelName, (err, data) => {
let msg = JSON.parse(data.payload)[1][0]; let msg = JSON.parse(data.payload)[1][0];
channels.addMessage(channelName, msg, data.data.sig, data.username) if (JSON.parse(data.payload)[1][1] === 'content/json') {
handleProtocolMessages(channelName, data);
} else {
channels.addMessage(channelName, msg, data.data.sig, data.username)
}
}); });
}) })

View File

@ -255,7 +255,7 @@ class UI {
width: '73%', width: '73%',
height: '5%', height: '5%',
left: '7%', left: '7%',
top: '92%', top: '90%',
border: { border: {
type: 'line' type: 'line'
}, },