]> git.sesse.net Git - ultimatescore/blob - pubsub/bodet.js
Add a comment about what “Mark game” means.
[ultimatescore] / pubsub / bodet.js
1 /*
2  * bodet.js
3  *
4  * Acts as a bridge for forwarding UDP packets on port 6000 to PubSub.
5  *
6  * Listens on UDP [::1]:6000 by default, can be overriden with the first and
7  * second command line argument, respectively. No legacy IP support.
8  *
9  * Forwards the port number as an attribute (with the name "port") to the
10  * pubsub messages, for ingestion of data from multiple bodet.js instances to
11  * ultimatescore.
12  *
13  * See README for required credential setup.
14  *
15  */
16
17 const {PubSub} = require('@google-cloud/pubsub');
18 const dgram = require('dgram');
19
20 const topic = 'projects/plastkast/topics/ultimatescore';
21 const host = process.argv[2] || '::1';
22 const port = process.argv[3] || 6000;
23
24 const pubsub = new PubSub();
25
26 const sock = dgram.createSocket('udp6');
27 sock.on('listening', () => {
28   const addr = sock.address().address;
29   console.log(`UDP listening on [${addr}]:${port}`);
30 });
31
32 sock.on('message', processPacket);
33
34 async function processPacket(msg, rinfo) {
35   console.log(`UDP datagram from ${rinfo.address}:${port}: ${msg}`);
36   const attr = { port: port.toString() };
37   const id = await pubsub.topic(topic).publish(msg, attr);
38   console.log(`PubSub message published with ID ${id}`);
39 };
40
41 sock.bind(port, host);