-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
42 lines (34 loc) · 1.09 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const RmqRpc = require('./index');
const user = process.env.RMQUSER || 'guest';
const password = process.env.RMQPASS || 'guest';
class Server {
async run() {
const mq = new RmqRpc(`amqp://${user}:${password}@localhost`);
// Create connection and channel
await mq.init();
// Initialize an exchange and queue 'square'
await mq.setupQueue('square');
// Wait for requests
mq.listenForMessages(async (request) => {
// Handle request
return Math.pow(Number(request), 2);
}).then(() => {
console.log('Server ready.')
});
}
}
class Client {
async run() {
const mq = new RmqRpc(`amqp://${user}:${password}@localhost`);
// Create connection and channel
await mq.init();
const num = Math.random();
// Send request and await result
const result = await mq.sendRpc('square', num);
console.log(`Num: ${num}, Square: ${result}`);
}
}
const server = new Server();
server.run();
const client = new Client();
client.run().then(() => process.exit());