Compare commits
No commits in common. "4fbbc73527ea5b8b61be4c8647465553bcfa9ee2" and "b88337c39827abbc3c139bb765fee33c6bddef7d" have entirely different histories.
4fbbc73527
...
b88337c398
3
app.js
3
app.js
@ -307,7 +307,7 @@ function sendMessage(e) {
|
||||
message,
|
||||
avatar: config.userAvatar,
|
||||
topic: b4a.toString(currentRoom.topic, 'hex'),
|
||||
peers: peersPublicKeys, // Deprecated. To be deleted in future updates
|
||||
peers: peersPublicKeys,
|
||||
timestamp: Date.now(),
|
||||
readableTimestamp: new Date().toLocaleString(), // Added human-readable timestamp
|
||||
});
|
||||
@ -327,7 +327,6 @@ function sendFileMessage(name, fileUrl, fileType, avatar) {
|
||||
fileUrl,
|
||||
fileType,
|
||||
avatar,
|
||||
topic: b4a.toString(currentRoom.topic, 'hex'),
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import fs from 'fs';
|
||||
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';
|
||||
|
||||
// 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
|
||||
bot.on('onMessage', (peer, message) => {
|
||||
if (message.type == "icon") return;
|
||||
console.log(message);
|
||||
|
||||
console.log(`Message received from ${message.name}@${message.topic} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`);
|
||||
|
@ -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;
|
@ -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;
|
@ -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;
|
@ -3,6 +3,8 @@ class ChatRoom {
|
||||
this.topic = topic;
|
||||
this.peers = peers;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
export default ChatRoom;
|
@ -1,6 +1,5 @@
|
||||
import Hyperswarm from 'hyperswarm';
|
||||
import EventEmitter from 'node:events'
|
||||
import b4a from "b4a";
|
||||
|
||||
class Client extends EventEmitter {
|
||||
constructor(botName) {
|
||||
@ -13,12 +12,7 @@ class Client extends EventEmitter {
|
||||
|
||||
setupSwarm() {
|
||||
this.swarm.on('connection', (peer) => {
|
||||
peer.on('data', message => {
|
||||
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('data', message => this.emit('onMessage', peer, JSON.parse(message.toString())));
|
||||
peer.on('error', e => {
|
||||
this.emit('onError', e);
|
||||
console.error(`Connection error: ${e}`)
|
||||
@ -26,16 +20,7 @@ class Client extends EventEmitter {
|
||||
});
|
||||
|
||||
this.swarm.on('update', () => {
|
||||
console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.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')));
|
||||
});
|
||||
console.log(`Peers count: ${this.swarm.connections.size}`);
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user