forked from snxraven/LinkUp-P2P-Chat
Merge pull request 'Fixed bot crash on file attachment, added 2 more new events and topic ID to file message' (#6) from MiTask/LinkUp-P2P-Chat:main into main
Reviewed-on: snxraven/LinkUp-P2P-Chat#6
This commit is contained in:
commit
4fbbc73527
3
app.js
3
app.js
@ -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,
|
peers: peersPublicKeys, // Deprecated. To be deleted in future updates
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
readableTimestamp: new Date().toLocaleString(), // Added human-readable timestamp
|
readableTimestamp: new Date().toLocaleString(), // Added human-readable timestamp
|
||||||
});
|
});
|
||||||
@ -327,6 +327,7 @@ function sendFileMessage(name, fileUrl, fileType, avatar) {
|
|||||||
fileUrl,
|
fileUrl,
|
||||||
fileType,
|
fileType,
|
||||||
avatar,
|
avatar,
|
||||||
|
topic: b4a.toString(currentRoom.topic, 'hex'),
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -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,7 +39,6 @@ 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}`);
|
||||||
|
@ -3,8 +3,6 @@ class ChatRoom {
|
|||||||
this.topic = topic;
|
this.topic = topic;
|
||||||
this.peers = peers;
|
this.peers = peers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ChatRoom;
|
export default ChatRoom;
|
@ -1,5 +1,6 @@
|
|||||||
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) {
|
||||||
@ -12,7 +13,12 @@ class Client extends EventEmitter {
|
|||||||
|
|
||||||
setupSwarm() {
|
setupSwarm() {
|
||||||
this.swarm.on('connection', (peer) => {
|
this.swarm.on('connection', (peer) => {
|
||||||
peer.on('data', message => this.emit('onMessage', peer, JSON.parse(message.toString())));
|
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('error', e => {
|
peer.on('error', e => {
|
||||||
this.emit('onError', e);
|
this.emit('onError', e);
|
||||||
console.error(`Connection error: ${e}`)
|
console.error(`Connection error: ${e}`)
|
||||||
@ -20,7 +26,16 @@ class Client extends EventEmitter {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.swarm.on('update', () => {
|
this.swarm.on('update', () => {
|
||||||
console.log(`Peers count: ${this.swarm.connections.size}`);
|
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')));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
12
chatBot/includes/FileMessage.js
Normal file
12
chatBot/includes/FileMessage.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
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;
|
10
chatBot/includes/TextMessage.js
Normal file
10
chatBot/includes/TextMessage.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
class TextMessage {
|
||||||
|
public TextMessage(chatRoom, userPeer, message, timestamp) {
|
||||||
|
this.chatRoom = chatRoom;
|
||||||
|
this.userPeer = userPeer;
|
||||||
|
this.message = message;
|
||||||
|
this.timestamp = timestamp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default TextMessage;
|
10
chatBot/includes/UserPeer.js
Normal file
10
chatBot/includes/UserPeer.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
class UserPeer {
|
||||||
|
public UserPeer(peer, topics, username, avatar) {
|
||||||
|
this.peer = peer;
|
||||||
|
this.topics = topics;
|
||||||
|
this.username = username;
|
||||||
|
this.avatar = avatar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default UserPeer;
|
Loading…
Reference in New Issue
Block a user