OpenROV /
openrov-cockpit
| 1 | "use strict"; |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 2 | const dgram = require('dgram'); |
||
|
0 ignored issues
–
show
|
|||
| 3 | const EventEmitter = require('events'); |
||
|
0 ignored issues
–
show
|
|||
| 4 | const os = require('os'); |
||
|
0 ignored issues
–
show
|
|||
| 5 | var ifaces = os.networkInterfaces(); |
||
| 6 | function getBroadcastAddress(addressString) { |
||
| 7 | var address = addressString.split('.'); |
||
| 8 | address[3] = '255'; |
||
| 9 | return address.join('.'); |
||
| 10 | } |
||
| 11 | |||
| 12 | class Beacon extends EventEmitter{ |
||
|
0 ignored issues
–
show
|
|||
| 13 | |||
| 14 | constructor (options){ |
||
| 15 | if(!options){ |
||
| 16 | options = {} |
||
|
0 ignored issues
–
show
There should be a semicolon.
Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers. Further Readings: Loading history...
|
|||
| 17 | } |
||
| 18 | super(); |
||
| 19 | this.port = options.port || 8088; |
||
| 20 | this.broadcastAddress = options.broadcastAddress || '192.168.1.255';//;'230.185.192.108'; |
||
| 21 | this.beaconRate = options.beaconRate || 3000; |
||
| 22 | |||
| 23 | } |
||
| 24 | |||
| 25 | broadcast(messageFn) { |
||
| 26 | var self = this; |
||
| 27 | this.server = dgram.createSocket('udp4'); |
||
| 28 | let server = this.server; |
||
|
0 ignored issues
–
show
|
|||
| 29 | server.on('listening', () => { |
||
|
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: Loading history...
|
|||
| 30 | var address = server.address(); |
||
| 31 | server.setBroadcast(true) |
||
|
0 ignored issues
–
show
There should be a semicolon.
Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers. Further Readings: Loading history...
|
|||
| 32 | // server.setMulticastTTL(128); |
||
| 33 | // server.addMembership( self.broadcastAddress); |
||
| 34 | }); |
||
| 35 | |||
| 36 | this.beaconInterval = setInterval(broadcastNew, this.beaconRate); |
||
|
0 ignored issues
–
show
|
|||
| 37 | |||
| 38 | function broadcastNew() { |
||
| 39 | ifaces = os.networkInterfaces(); //In case interfaces change; |
||
| 40 | //Updated to iterate over all ipv4 interfaces |
||
| 41 | var message = new Buffer(JSON.stringify(messageFn())); |
||
|
0 ignored issues
–
show
|
|||
| 42 | Object.keys(ifaces).forEach(function(iface) { |
||
| 43 | ifaces[iface].forEach(function(idetail) { |
||
| 44 | if('IPv4' !== idetail.family || idetail.internal !== false) { |
||
| 45 | //Skip over internal (i.e. 127.0.0.1) and non ipv4 |
||
| 46 | return; |
||
| 47 | } |
||
| 48 | idetail.broadcastAddress = getBroadcastAddress(idetail.address); |
||
| 49 | server.send(message, 0, message.length, self.port, idetail.broadcastAddress); |
||
| 50 | }); |
||
| 51 | }); |
||
| 52 | } |
||
| 53 | server.bind(); |
||
| 54 | } |
||
| 55 | |||
| 56 | listen () { |
||
| 57 | var self=this; |
||
| 58 | this.client = dgram.createSocket('udp4'); |
||
| 59 | let client = this.client; |
||
|
0 ignored issues
–
show
|
|||
| 60 | |||
| 61 | client.on('listening', function () { |
||
| 62 | var address = client.address(); |
||
| 63 | console.log('UDP Client listening on ' + address.address + ":" + address.port); |
||
|
0 ignored issues
–
show
|
|||
| 64 | client.setBroadcast(true) |
||
|
0 ignored issues
–
show
There should be a semicolon.
Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers. Further Readings: Loading history...
|
|||
| 65 | // client.setMulticastTTL(128); |
||
| 66 | // client.addMembership( self.broadcastAddress); |
||
| 67 | }); |
||
| 68 | |||
| 69 | client.on('message', function (message, remote) { |
||
| 70 | self.emit("deviceAnnouncement",{remoteAddress:remote.address,remotePort:remote.port,data:message}) |
||
|
0 ignored issues
–
show
There should be a semicolon.
Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers. Further Readings: Loading history...
|
|||
| 71 | }); |
||
| 72 | |||
| 73 | client.bind(this.port); |
||
| 74 | |||
| 75 | } |
||
| 76 | |||
| 77 | stop () { |
||
| 78 | clearInterval(this.beaconInterval); |
||
|
0 ignored issues
–
show
|
|||
| 79 | if (this.server){ |
||
| 80 | this.server.close(); |
||
| 81 | } |
||
| 82 | if (this.client){ |
||
| 83 | this.client.close(); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | module.exports = function(options){ |
||
|
0 ignored issues
–
show
|
|||
| 89 | return new Beacon(options) |
||
|
0 ignored issues
–
show
There should be a semicolon.
Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers. Further Readings: Loading history...
|
|||
| 90 | }; |
||
| 91 |