Add full support for multiple connections with one account in Socket.IO

Add LIST command
merge-requests/6/head
knotteye 2019-12-04 19:06:56 -06:00
parent 661d953919
commit be38b873eb
3 changed files with 31 additions and 8 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "satyr", "name": "satyr",
"version": "0.5.2", "version": "0.5.3",
"description": "A livestreaming server.", "description": "A livestreaming server.",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"author": "knotteye", "author": "knotteye",

View File

@ -13,6 +13,7 @@ import { readFileSync, writeFileSync } from "fs";
import { JWT, JWK } from "jose"; import { JWT, JWK } from "jose";
import { strict } from "assert"; import { strict } from "assert";
import { parse } from "path"; import { parse } from "path";
import { isBuffer } from "util";
const app = express(); const app = express();
const server = http.createServer(app); const server = http.createServer(app);
@ -77,13 +78,13 @@ async function newNick(socket, skip?: boolean) {
} }
} }
async function chgNick(socket, nick) { async function chgNick(socket, nick, f?: boolean) {
let rooms = Object.keys(socket.rooms); let rooms = Object.keys(socket.rooms);
for(let i=1;i<rooms.length;i++){ for(let i=1;i<rooms.length;i++){
io.to(rooms[i]).emit('ALERT', socket.nick+' is now known as '+nick); io.to(rooms[i]).emit('ALERT', socket.nick+' is now known as '+nick);
} }
store.rm(socket.nick); if(store.get(socket.nick)) store.rm(socket.nick);
store.set(nick, socket.id); if (!f) store.set(nick, socket.id);
socket.nick = nick; socket.nick = nick;
} }
@ -195,7 +196,7 @@ async function initAPI() {
if(t) { if(t) {
if(t['exp'] - 86400 < Math.floor(Date.now() / 1000)){ if(t['exp'] - 86400 < Math.floor(Date.now() / 1000)){
return genToken(t['username']).then((t) => { return genToken(t['username']).then((t) => {
res.cookie('Authorization', t); res.cookie('Authorization', t, {maxAge: 604800000, httpOnly: true});
res.send('{"success":""}'); res.send('{"success":""}');
return; return;
}); });
@ -214,7 +215,7 @@ async function initAPI() {
api.login(req.body.username, req.body.password).then((result) => { api.login(req.body.username, req.body.password).then((result) => {
if(!result){ if(!result){
genToken(req.body.username).then((t) => { genToken(req.body.username).then((t) => {
res.cookie('Authorization', t); res.cookie('Authorization', t, {maxAge: 604800000, httpOnly: true});
res.send('{"success":""}'); res.send('{"success":""}');
}) })
} }
@ -348,6 +349,21 @@ async function initChat(ircconf: any) {
} }
else socket.emit('ALERT', 'Room does not exist'); else socket.emit('ALERT', 'Room does not exist');
}); });
socket.on('LIST', (data) => {
let str = "";
let client;
io.in(data.room).clients((err, clients) => {
if(err) throw err;
if(clients === []) {
socket.emit('LIST', 'The room is empty.');
return;
}
for(let i=0;i<clients.length;i++) {
str += io.sockets.connected[clients[i]].nick+', ';
}
socket.emit('LIST', str.substring(0, str.length - 2));
});
});
socket.on('LEAVEROOM', (data) => { socket.on('LEAVEROOM', (data) => {
socket.leave(data); socket.leave(data);
if(ircconf.enable) irc.part(socket.nick, data); if(ircconf.enable) irc.part(socket.nick, data);
@ -375,7 +391,7 @@ async function initChat(ircconf: any) {
return false; return false;
} }
if(await db.validatePassword(data.nick, data.password)){ if(await db.validatePassword(data.nick, data.password)){
chgNick(socket, data.nick); chgNick(socket, data.nick, true);
} }
else socket.emit('ALERT','Incorrect username or password'); else socket.emit('ALERT','Incorrect username or password');
} }
@ -404,4 +420,4 @@ async function initChat(ircconf: any) {
}); });
} }
export { init }; export { init };

View File

@ -33,6 +33,9 @@
else if(m.startsWith('/kick')){ else if(m.startsWith('/kick')){
socket.emit('KICK', {nick: m.split(' ')[1], room: room}); socket.emit('KICK', {nick: m.split(' ')[1], room: room});
} }
else if(m.startsWith('/list')){
socket.emit('LIST', {room: room});
}
else socket.emit('MSG', {room: room, msg: m}); else socket.emit('MSG', {room: room, msg: m});
document.getElementById('m').value = ''; document.getElementById('m').value = '';
} }
@ -52,6 +55,10 @@
document.getElementById('messages').innerHTML+='<li><i>'+data.nick+' has left the chat</i></li>'; document.getElementById('messages').innerHTML+='<li><i>'+data.nick+' has left the chat</i></li>';
window.scrollTo(0,document.body.scrollHeight); window.scrollTo(0,document.body.scrollHeight);
}); });
socket.on('LIST', function(data){
document.getElementById('messages').innerHTML+='<li><i>'+data+'</i></li>';
window.scrollTo(0,document.body.scrollHeight);
});
function getUrlParameter(name) { function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'); var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');