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() { async function initialize() {
try { try {
servePort = getRandomPort(); servePort = getRandomPort();
@ -493,7 +499,9 @@ function leaveRoom(topic) {
document.querySelector('#setup').classList.remove('hidden'); document.querySelector('#setup').classList.remove('hidden');
} }
} }
function sendMessage(e) {
async function sendMessage(e) {
e.preventDefault(); e.preventDefault();
const message = document.querySelector('#message').value; const message = document.querySelector('#message').value;
document.querySelector('#message').value = ''; document.querySelector('#message').value = '';
@ -503,11 +511,13 @@ function sendMessage(e) {
if (message.startsWith('~')) { if (message.startsWith('~')) {
// Handle command // Handle command
handleCommand(message, { await handleCommand(message, {
eventEmitter, eventEmitter,
currentTopic, currentTopic,
clearMessages, 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; return;
} }

View File

@ -11,17 +11,34 @@ if (fs.existsSync(agentAvatarPath)) {
agentAvatar = `data:image/png;base64,${b4a.toString(avatarBuffer, 'base64')}`; agentAvatar = `data:image/png;base64,${b4a.toString(avatarBuffer, 'base64')}`;
} }
export default function handleCommand(command, context) { export default async function handleCommand(command, context) {
const { eventEmitter, currentTopic, clearMessages, addMessage } = 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': case '~clear':
clearMessages(); clearMessages();
break; break;
case '~ping': case '~ping':
addMessage('LinkUp', 'pong', agentAvatar, currentTopic()); addMessage('LinkUp', 'pong', agentAvatar, currentTopic());
break; 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: default:
console.log('Unknown command:', command); console.log('Unknown command:', command);
} }
} }