Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var useTLS = window.location.protocol === 'https:'
var host = window.location.hostname;
var port = parseInt(window.location.port);
if (!Number.isInteger(port)) {
port = (useTLS) ? 443 : 80;
}
const signalkServer = getUrlParameter("signalkServer");
if (typeof signalkServer !== 'undefined') {
useTLS = signalkServer.startsWith('wss://');
host = signalkServer.split(':')[1].replace(/\//g, '');
port = parseInt(signalkServer.split(':')[2]);
if (!Number.isInteger(port)) {
port = (useTLS) ? 443 : 80;
}
}
this.SignalkClient = new Client({
hostname: host,
port: port,
useTLS: useTLS,
reconnect: true,
autoConnect: false,
notifications: false,
maxRetries: 10
});
this.loadPreferredUnits();
}
subscribe: [{path: '*', period: 10000}]
})
console.log(`SignalK connected to ${parseAddress(address)}`)
}
const onDisconnect = c => {
c && c.close && c.close()
rawStream.end()
connectionStateBus.push('disconnected')
console.log('SignalK disconnected')
}
const onError = e => {
rawStream.push(new Bacon.Error(e))
onDisconnect()
}
const signalk = new SignalK.Client()
signalk.connectDelta(parseAddress(address), onMessage, onConnect, onDisconnect, onError, onDisconnect, 'none')
const selfStream = rawStream.filter(msg => isSelf(msg.context))
const updates = selfStream.map(msg => {
return _(msg.updates)
.map(u =>
_.map(u.values, v => {
return {
timestamp: u.timestamp,
path: v.path,
value: v.value
}
})
)
.flatten()
.value()
MdnsWs.prototype.connect = function (discovery) {
if (this.remoteServers[discovery.host + ':' + discovery.port]) {
debug(
'Discovered ' +
discovery.host +
':' +
discovery.port +
' already known, not connecting'
)
return
}
const signalkClient = new SignalK.Client()
let url
if (discovery.discoveryResponse) {
_object.values(discovery.discoveryResponse.endpoints)[0]['signalk-ws']
} else {
const protocol = discovery.protocol || 'ws'
url =
protocol +
'://' +
discovery.host +
':' +
discovery.port +
'/signalk/v1/stream?subscribe=all'
}
const that = this
const providerId = `${that.options.providerId}.${discovery.host}:${
objectMode: true
})
this.options = options
this.selfHost = options.app.config.getExternalHostname() + '.'
this.selfPort = options.app.config.getExternalPort()
this.remoteServers = {}
this.remoteServers[this.selfHost + ':' + this.selfPort] = {}
if (options.ignoreServers) {
options.ignoreServers.forEach(s => {
this.remoteServers[s] = {}
})
}
if (options.host && options.port) {
this.connect(options)
} else {
this.signalkClient = new SignalK.Client()
this.signalkClient.on('discovery', this.connect.bind(this))
this.signalkClient.on('discoveryError', info => {
const providerId = `${options.providerId}.${info.host}:${info.port}`
options.app.setProviderError(providerId, info.error.message)
})
debug('Starting discovery')
this.signalkClient.startDiscovery()
}
}