Compare commits

..

No commits in common. "4fbbc73527ea5b8b61be4c8647465553bcfa9ee2" and "b88337c39827abbc3c139bb765fee33c6bddef7d" have entirely different histories.

7 changed files with 7 additions and 52 deletions

3
app.js
View File

@ -307,7 +307,7 @@ function sendMessage(e) {
message, message,
avatar: config.userAvatar, avatar: config.userAvatar,
topic: b4a.toString(currentRoom.topic, 'hex'), topic: b4a.toString(currentRoom.topic, 'hex'),
peers: peersPublicKeys, // Deprecated. To be deleted in future updates peers: peersPublicKeys,
timestamp: Date.now(), timestamp: Date.now(),
readableTimestamp: new Date().toLocaleString(), // Added human-readable timestamp readableTimestamp: new Date().toLocaleString(), // Added human-readable timestamp
}); });
@ -327,7 +327,6 @@ function sendFileMessage(name, fileUrl, fileType, avatar) {
fileUrl, fileUrl,
fileType, fileType,
avatar, avatar,
topic: b4a.toString(currentRoom.topic, 'hex'),
timestamp: Date.now(), timestamp: Date.now(),
}); });

View File

@ -1,6 +1,6 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import Client from './includes/Client.js'; // Adjust the import path as necessary import Client from './includes/client.js'; // Adjust the import path as necessary
import 'dotenv/config'; import 'dotenv/config';
// Create a new instance of the chatBot class with a valid botName // Create a new instance of the chatBot class with a valid botName
@ -39,6 +39,7 @@ loadCommands().then(commands => {
// We use Event Emitter here to handle new messages // We use Event Emitter here to handle new messages
bot.on('onMessage', (peer, message) => { bot.on('onMessage', (peer, message) => {
if (message.type == "icon") return;
console.log(message); console.log(message);
console.log(`Message received from ${message.name}@${message.topic} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`); console.log(`Message received from ${message.name}@${message.topic} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`);

View File

@ -1,12 +0,0 @@
class FileMessage {
public FileMessage(chatRoom, userPeer, fileName, fileUrl, fileType, timestamp) {
this.chatRoom = chatRoom;
this.userPeer = userPeer;
this.fileName = fileName;
this.fileUrl = fileUrl;
this.fileType = fileType;
this.timestamp = timestamp;
}
}
export default FileMessage;

View File

@ -1,10 +0,0 @@
class TextMessage {
public TextMessage(chatRoom, userPeer, message, timestamp) {
this.chatRoom = chatRoom;
this.userPeer = userPeer;
this.message = message;
this.timestamp = timestamp;
}
}
export default TextMessage;

View File

@ -1,10 +0,0 @@
class UserPeer {
public UserPeer(peer, topics, username, avatar) {
this.peer = peer;
this.topics = topics;
this.username = username;
this.avatar = avatar;
}
}
export default UserPeer;

View File

@ -3,6 +3,8 @@ class ChatRoom {
this.topic = topic; this.topic = topic;
this.peers = peers; this.peers = peers;
} }
} }
export default ChatRoom; export default ChatRoom;

View File

@ -1,6 +1,5 @@
import Hyperswarm from 'hyperswarm'; import Hyperswarm from 'hyperswarm';
import EventEmitter from 'node:events' import EventEmitter from 'node:events'
import b4a from "b4a";
class Client extends EventEmitter { class Client extends EventEmitter {
constructor(botName) { constructor(botName) {
@ -13,12 +12,7 @@ class Client extends EventEmitter {
setupSwarm() { setupSwarm() {
this.swarm.on('connection', (peer) => { this.swarm.on('connection', (peer) => {
peer.on('data', message => { peer.on('data', message => this.emit('onMessage', peer, JSON.parse(message.toString())));
if(message.type === "message") this.emit('onMessage', peer, JSON.parse(message.toString()));
if(message.type === "icon") this.emit('onIcon', peer, JSON.parse(message.toString()));
if(message.type === "file") this.emit('onFile', peer, JSON.parse(message.toString()));
});
peer.on('error', e => { peer.on('error', e => {
this.emit('onError', e); this.emit('onError', e);
console.error(`Connection error: ${e}`) console.error(`Connection error: ${e}`)
@ -26,16 +20,7 @@ class Client extends EventEmitter {
}); });
this.swarm.on('update', () => { this.swarm.on('update', () => {
console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`); console.log(`Peers count: ${this.swarm.connections.size}`);
this.swarm.peers.forEach((peerInfo, peerId) => {
// Please do not try to understand what is going on here. I have no idea anyway. But it surprisingly works
const peer = [peerId];
const peerTopics = [peerInfo.topics]
.filter(topics => topics)
.map(topics => topics.map(topic => b4a.toString(topic, 'hex')));
});
}); });
} }