-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_server.js
More file actions
36 lines (30 loc) · 1.16 KB
/
05_server.js
File metadata and controls
36 lines (30 loc) · 1.16 KB
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
var queueSocket = "amqp://localhost";
var queueName = "nodejs.demo.addition";
var encoding = "utf8";
// Create a new context.
var context = require("rabbit.js").createContext(queueSocket);
console.log(" [x] Created context");
// When the context is open...
context.on("ready", function () {
console.log(" [x] Context is ready");
// Create a reply socket.
var rep = context.socket("REP");
console.log(" [x] Created reply socket");
// The following callback will be invoked when a request is sent to the queue.
rep.on("data", function (data) {
console.log(" [x] Data received: %s", data);
data = JSON.parse(data);
// Create the response object and write it to the reply.
// Adding setTimeout so it purposely takes a long time.
var durationInMs = parseInt(Math.random() * 10000, 10);
setTimeout(function () {
var response = { id: data.id, x: data.x, y: data.y, sum: data.x + data.y };
var content = JSON.stringify(response);
rep.write(content, encoding);
}, durationInMs);
});
// Connect the reply socket to the queue.
rep.connect(queueName, function () {
console.log(" [x] Connected to queue: %s", queueName);
});
});