2024-06-03 19:23:14 -04:00
import Hyperswarm from 'hyperswarm' ;
2024-06-08 15:34:25 -04:00
import EventEmitter from 'node:events'
2024-06-10 14:09:33 -04:00
import b4a from "b4a" ;
2024-06-03 19:23:14 -04:00
2024-06-08 15:34:25 -04:00
class Client extends EventEmitter {
2024-06-09 03:51:15 -04:00
constructor ( botName ) {
2024-06-08 15:34:25 -04:00
super ( ) ;
2024-06-09 03:51:15 -04:00
if ( ! botName ) return console . error ( "BotName is not defined!" ) ;
2024-06-03 19:23:14 -04:00
this . botName = botName ;
this . swarm = new Hyperswarm ( ) ;
this . setupSwarm ( ) ;
}
setupSwarm ( ) {
this . swarm . on ( 'connection' , ( peer ) => {
2024-06-08 16:31:24 -04:00
peer . on ( 'data' , message => this . emit ( 'onMessage' , peer , JSON . parse ( message . toString ( ) ) ) ) ;
2024-06-08 16:29:51 -04:00
peer . on ( 'error' , e => {
2024-06-08 16:31:24 -04:00
this . emit ( 'onError' , e ) ;
2024-06-08 16:29:51 -04:00
console . error ( ` Connection error: ${ e } ` )
} ) ;
2024-06-03 19:23:14 -04:00
} ) ;
this . swarm . on ( 'update' , ( ) => {
2024-06-10 13:57:32 -04:00
console . log ( ` Connections count: ${ this . swarm . connections . size } / Peers count: ${ this . swarm . peers . size } ` ) ;
2024-06-10 13:46:59 -04:00
2024-06-10 14:00:22 -04:00
this . swarm . peers . forEach ( ( peerInfo , peer ) => {
2024-06-10 14:10:25 -04:00
console . log ( ` Peer ${ [ peer ] } is in topic(s) [ ${ [ peerInfo . topics ] . filter ( topic => topic ) . map ( topic => {
console . log ( topic )
2024-06-10 14:11:46 -04:00
return "test"
// b4a.toString(topic, 'hex')
2024-06-10 14:10:25 -04:00
} ) . join ( ", " ) } ] ` );
2024-06-10 13:46:59 -04:00
} ) ;
2024-06-03 19:23:14 -04:00
} ) ;
}
2024-06-09 03:51:15 -04:00
joinChatRoom ( chatRoomID ) {
this . discovery = this . swarm . join ( Buffer . from ( chatRoomID , 'hex' ) , { client : true , server : true } ) ;
2024-06-03 19:23:14 -04:00
this . discovery . flushed ( ) . then ( ( ) => {
console . log ( ` Bot ${ this . botName } joined the chat room. ` ) ;
2024-06-08 16:36:00 -04:00
this . emit ( 'onBotJoinRoom' ) ;
2024-06-03 19:23:14 -04:00
} ) ;
}
2024-06-09 15:55:32 -04:00
sendMessage ( roomPeers , message ) {
2024-06-03 19:23:14 -04:00
console . log ( 'Bot name:' , this . botName ) ;
const timestamp = Date . now ( ) ; // Generate timestamp
2024-06-10 10:28:18 -04:00
const peers = [ ... this . swarm . connections ] . filter ( peer => roomPeers . includes ( peer . remotePublicKey . toString ( 'hex' ) ) ) ; // We remove all the peers that arent included in a room
const data = JSON . stringify ( { name : this . botName , message , timestamp } ) ; // Include timestamp
for ( const peer of peers ) peer . write ( data ) ;
}
sendMessageToAll ( message ) {
console . log ( 'Bot name:' , this . botName ) ;
const timestamp = Date . now ( ) ; // Generate timestamp
const peers = [ ... this . swarm . connections ]
2024-06-08 16:31:24 -04:00
const data = JSON . stringify ( { name : this . botName , message , timestamp } ) ; // Include timestamp
2024-06-03 19:23:14 -04:00
for ( const peer of peers ) peer . write ( data ) ;
}
destroy ( ) {
this . swarm . destroy ( ) ;
console . log ( ` Bot ${ this . botName } disconnected. ` ) ;
}
}
2024-06-08 16:31:24 -04:00
export default Client ;