forked from snxraven/LinkUp-P2P-Chat
Up to date fork #1
16
app.js
16
app.js
@ -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;
|
||||
}
|
||||
|
25
commands.js
25
commands.js
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user