Up to date fork #1

Merged
MiTask merged 33 commits from snxraven/LinkUp-P2P-Chat:main into main 2024-06-14 04:32:25 -04:00
2 changed files with 34 additions and 7 deletions
Showing only changes of commit 458954c1e1 - Show all commits

16
app.js
View File

@ -61,6 +61,12 @@ function updatePeerCount() {
}
}
async function joinRoom(topicStr) {
const topicBuffer = b4a.from(topicStr, 'hex');
addRoomToList(topicStr);
await joinSwarm(topicBuffer);
}
async function initialize() {
try {
servePort = getRandomPort();
@ -493,7 +499,9 @@ function leaveRoom(topic) {
document.querySelector('#setup').classList.remove('hidden');
}
}
function sendMessage(e) {
async function sendMessage(e) {
e.preventDefault();
const message = document.querySelector('#message').value;
document.querySelector('#message').value = '';
@ -503,11 +511,13 @@ function sendMessage(e) {
if (message.startsWith('~')) {
// Handle command
handleCommand(message, {
await handleCommand(message, {
eventEmitter,
currentTopic,
clearMessages,
addMessage: (from, message, avatar, topic) => onMessageAdded(from, message, avatar, topic, timestamp)
addMessage: (from, message, avatar, topic) => onMessageAdded(from, message, avatar, topic, timestamp),
joinRoom,
leaveRoom
});
return;
}

View File

@ -11,17 +11,34 @@ if (fs.existsSync(agentAvatarPath)) {
agentAvatar = `data:image/png;base64,${b4a.toString(avatarBuffer, 'base64')}`;
}
export default function handleCommand(command, context) {
const { eventEmitter, currentTopic, clearMessages, addMessage } = context;
export default async function handleCommand(command, context) {
const { eventEmitter, currentTopic, clearMessages, addMessage, joinRoom, leaveRoom } = context;
const args = command.trim().split(' ');
const cmd = args[0].toLowerCase();
const restArgs = args.slice(1).join(' ');
switch (command.trim().toLowerCase()) {
switch (cmd) {
case '~clear':
clearMessages();
break;
case '~ping':
addMessage('LinkUp', 'pong', agentAvatar, currentTopic());
break;
case '~help':
addMessage('LinkUp', 'Available commands: ~clear, ~ping, ~help, ~join [topic], ~leave', agentAvatar, currentTopic());
break;
case '~join':
if (restArgs) {
await joinRoom(restArgs);
} else {
addMessage('LinkUp', 'Usage: ~join [topic]', agentAvatar, currentTopic());
}
break;
case '~leave':
leaveRoom(currentTopic());
break;
default:
console.log('Unknown command:', command);
}
}
}