Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6a7f6f2

Browse files
committedMay 15, 2024
Having some trouble getting Cabal to emit its 'init' event
1 parent e15da64 commit 6a7f6f2

File tree

2 files changed

+127
-15
lines changed

2 files changed

+127
-15
lines changed
 

‎src/main.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
import Client from 'cabal-client';
1+
import { Server } from './server.js';
22

3-
const client = new Client({
4-
config: {
5-
dbdir: '/tmp/cabals'
6-
}
7-
})
3+
if (process.argv.length < 3) {
4+
throw new Error('Usage: npm start <name> <neighbour1> <neighbour2> ...');
5+
}
6+
const name = process.argv[2];
87

9-
client.addCabal('cabal://411dbe21d6f8e222733ac88d2da3cf953aa9f5466db94fa9fa967a765be3875e').then((cabal) => {
10-
console.log(cabal.key);
11-
cabal.on('new-message', (event) => {
12-
console.log(event.channel, event.author.key, event.message.value.content.text);
13-
});
14-
cabal.processLine('/join default');
15-
cabal.processLine('hello cabal');
16-
// resolves when the cabal is ready, returns a CabalDetails instance
17-
});
8+
const server = new Server(name);
9+
server.testReceive();
10+
// for (let i = 3; i < process.argv.length; i++) {
11+
// console.log(`Adding neighbour ${process.argv[i]}`);
12+
// await server.addNeighbour(process.argv[i]);
13+
// }

‎src/server.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import Client from 'cabal-client';
2+
import { createHmac } from "node:crypto";
3+
4+
function generateHypercoreKey(input: string): string {
5+
return createHmac('sha256', input).digest('hex');
6+
}
7+
8+
// const client = new Client({
9+
// config: {
10+
// dbdir: '/tmp/cabals'
11+
// }
12+
// })
13+
14+
// console.log(process.argv);
15+
16+
// client.addCabal('cabal://411dbe21d6f8e222733ac88d2da3cf953aa9f5466db94fa9fa967a765be3875e').then((cabal) => {
17+
// console.log(cabal.key);
18+
// cabal.on('new-message', (event) => {
19+
// console.log(event.channel, event.author.key, event.message.value.content.text);
20+
// });
21+
// cabal.processLine('/join default');
22+
// cabal.processLine('hello cabal');
23+
// // resolves when the cabal is ready, returns a CabalDetails instance
24+
// });
25+
26+
export class Server {
27+
name: string;
28+
neighbours: {
29+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
30+
[name: string]: any // FIXME: would like to use [CabalDetails](https://github.com/cabal-club/cabal-client/blob/master/src/cabal-details.js) as the type here
31+
} = {};
32+
client: Client;
33+
constructor(name: string) {
34+
console.log('Server started');
35+
this.name = name;
36+
this.client = new Client({
37+
config: {
38+
dbdir: `/tmp/cabals-${this.name}`
39+
}
40+
});
41+
}
42+
testInitialize(): void {
43+
const client = new Client()
44+
45+
// the client is the interface for initializing cabals while
46+
// cabalDetails contains all information and methods for one specific cabal
47+
client.createCabal().then((cabalDetails) => {
48+
console.log('Cabal created', cabalDetails.key);
49+
// each cabal is an event emitter
50+
client.on('init', () => {
51+
console.log('Yay, I\'m ready!')
52+
// the key is the unique identifier of this cabal
53+
console.log('My key: ' + cabalDetails.key)
54+
})
55+
})
56+
}
57+
testReceive(): void {
58+
// we have two clients in this example, one for sending and one for recieving
59+
const client = new Client()
60+
const client2 = new Client()
61+
62+
client.createCabal().then((cabalDetails) => {
63+
cabalDetails.on('new-message', ({ channel, message }) => {
64+
console.log('Recieved: "' + message.value.content.text + '" in channel ' + channel)
65+
})
66+
67+
client.on('init', () => {
68+
client2.addCabal(cabalDetails.key).then((cabalDetails2) => {
69+
// both clients are now connected to the cabal
70+
cabalDetails2.on('init', () => {
71+
// this tells the other clients how we want to be called
72+
cabalDetails2.publishNick('CabalUser10', () => {
73+
// every new cabal has a channel named default
74+
cabalDetails2.publishMessage({
75+
type: 'chat/text',
76+
content: {
77+
text: 'Hey there!',
78+
channel: 'default'
79+
}
80+
})
81+
82+
// other channels will be created when we start using them
83+
cabalDetails2.publishMessage({
84+
type: 'chat/text',
85+
content: {
86+
text: 'People call me ' + cabalDetails2.getLocalName(),
87+
channel: 'introduction'
88+
}
89+
})
90+
})
91+
})
92+
})
93+
})
94+
})
95+
}
96+
async addNeighbour(name: string): Promise<void> {
97+
const channelName = generateHypercoreKey([this.name, name].sort().join('-'));
98+
console.log(channelName);
99+
this.neighbours[name] = await this.client.addCabal(`cabal://${channelName}`);
100+
console.log(`connecting to default channel of cabal between ${this.name} and ${name}, awaiting in init event`);
101+
await new Promise((resolve) => {
102+
this.neighbours[name].on('init', resolve);
103+
});
104+
console.log('Cabal is ready', this.neighbours[name].key, this.neighbours[name].getChannels());
105+
this.neighbours[name].on('new-message', (event) => {
106+
console.log(event.channel, event.author.name, event.message.value.content.text);
107+
});
108+
console.log('Joining default channel and sending hello message to neighbour');
109+
await this.neighbours[name].processLine('/join default');
110+
console.log(`Setting nick to ${this.name} in neighbour cabal`);
111+
await this.neighbours[name].processLine(`/nick ${this.name}`);
112+
console.log(`Sending hello message to neighbour ${name}`);
113+
await this.neighbours[name].processLine(`hello ${name}`);
114+
console.log('Message sent');
115+
}
116+
}

0 commit comments

Comments
 (0)
Please sign in to comment.