-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
60 lines (52 loc) · 1.51 KB
/
main.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const amqp_consumer = require("./amqp_consumer").amqp_consumer;
const amqp_publisher = require("./amqp_publisher").amqp_publisher;
let http = require("http");
let WSServer = require("websocket").server;
var count = 0;
var clients = [];
async function WSConnect() {
try {
console.log("[*] WS ready");
let amqpResponses = await amqp_consumer();
let amqpPublisher = await amqp_publisher();
console.log("[*] rabbitMQ ready");
wsServer.on("request", async req => {
let connection = req.accept(null, req.origin);
let id = count++;
clients[id] = connection;
console.log(`connection accepted, clientId ${id}`);
amqpResponses(rspAMQP => {
clients.forEach(client => {
client.sendUTF(rspAMQP);
});
});
connection.on("message", message => {
var msgString = message.utf8Data;
clients.forEach(client => {
client.sendUTF(msgString);
});
amqpPublisher(msgString);
});
connection.on("close", (reason, desc) => {
delete clients[id];
console.log(
`${new Date()} client ${id} : ${
connection.remoteAddress
} disconnected ${reason},${desc}`
);
});
});
} catch (err) {
console.log("ERR: " + err);
}
}
let server = http.createServer((req, resp) => {});
let wsServer = new WSServer({
httpServer: server
});
server.listen(1234, () =>
console.log(`${new Date()} Server is listening on port 1234`)
);
(async function() {
await WSConnect();
})();