forked from snxraven/LinkUp-P2P-Chat
Made Client.js->setupSwarm() parsing messages more readable
This commit is contained in:
parent
42dccf9547
commit
f706e7d621
@ -1,11 +1,11 @@
|
|||||||
import Hyperswarm from 'hyperswarm';
|
import Hyperswarm from 'hyperswarm';
|
||||||
import EventEmitter from 'node:events';
|
import EventEmitter from 'node:events';
|
||||||
import b4a from "b4a";
|
import b4a from "b4a";
|
||||||
import TextMessage from "./TextMessage.js";
|
import TextMessage from "./message/TextMessage.js";
|
||||||
import FileMessage from "./FileMessage.js";
|
import FileMessage from "./message/FileMessage.js";
|
||||||
import AudioMessage from "./AudioMessage.js";
|
import AudioMessage from "./message/AudioMessage.js";
|
||||||
import Message from "./Message.js";
|
import Message from "./message/Message.js";
|
||||||
import IconMessage from "./IconMessage.js";
|
import IconMessage from "./message/IconMessage.js";
|
||||||
|
|
||||||
class Client extends EventEmitter {
|
class Client extends EventEmitter {
|
||||||
constructor(botName) {
|
constructor(botName) {
|
||||||
@ -52,17 +52,22 @@ class Client extends EventEmitter {
|
|||||||
if (this.joinedRooms.has(messageObj.topic)) { // Process message only if it is from a joined room
|
if (this.joinedRooms.has(messageObj.topic)) { // Process message only if it is from a joined room
|
||||||
this.currentTopic = messageObj.topic; // Set the current topic from the incoming message
|
this.currentTopic = messageObj.topic; // Set the current topic from the incoming message
|
||||||
|
|
||||||
if (messageObj.type === "message")
|
const msgType = messageObj.type;
|
||||||
this.emit('onMessage', peer, new TextMessage(messageObj.name, messageObj.avatar, messageObj.topic, messageObj.timestamp, messageObj.message));
|
const peerName = messageObj.name;
|
||||||
|
const peerAvatar = messageObj.avatar;
|
||||||
|
const timestamp = messageObj.timestamp;
|
||||||
|
|
||||||
if (messageObj.type === "file")
|
if (msgType === "message")
|
||||||
this.emit('onFile', peer, new FileMessage(messageObj.name, messageObj.avatar, messageObj.avatar, messageObj.topic, messageObj.timestamp, messageObj.fileName, messageObj.fileUrl, messageObj.fileType));
|
this.emit('onMessage', peer, new TextMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.message));
|
||||||
|
|
||||||
if (messageObj.type === "icon")
|
if (msgType === "file")
|
||||||
this.emit('onIcon', peer, messageObj);
|
this.emit('onFile', peer, new FileMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.fileName, messageObj.fileUrl, messageObj.fileType));
|
||||||
|
|
||||||
if (messageObj.type === "audio")
|
if (msgType === "icon")
|
||||||
this.emit('onAudio', peer, new AudioMessage(messageObj.name, messageObj.avatar, messageObj.avatar, messageObj.topic, messageObj.timestamp, messageObj.audio, messageObj.audioType));
|
this.emit('onIcon', peer, new IconMessage(peerName, peerAvatar, timestamp));
|
||||||
|
|
||||||
|
if (msgType === "audio")
|
||||||
|
this.emit('onAudio', peer, new AudioMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.audio, messageObj.audioType));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import Message from "./Message.js";
|
import Message from "./Message.js";
|
||||||
|
import b4a from "b4a";
|
||||||
|
|
||||||
class IconMessage extends Message {
|
class IconMessage extends Message {
|
||||||
constructor(peerName, avatarBuffer, topic, timestamp) {
|
constructor(peerName, peerAvatar, topic, timestamp) {
|
||||||
super("icon", peerName, avatarBuffer, topic, timestamp);
|
super("icon", peerName, peerAvatar, topic, timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
toJsonString() {
|
toJsonString() {
|
||||||
@ -12,7 +13,7 @@ class IconMessage extends Message {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static new(bot, avatarBuffer) {
|
static new(bot, avatarBuffer) {
|
||||||
return new IconMessage(bot.botName, avatarBuffer, bot.currentTopic, Date.now());
|
return new IconMessage(bot.botName, b4a.toString(avatarBuffer, 'base64'), Date.now());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user