@signalk/signalk-clientdeprecated

Signal K JavaScript Client

Usage no npm install needed!

<script type="module">
  import signalkSignalkClient from 'https://cdn.skypack.dev/@signalk/signalk-client';
</script>

README

Signal K JavaScript Client Library

Installation

$ npm install signalk-client

Mdns is an optional dependency for doing automatic discovery. It is not relevant in browser environment, but Browserify will fail if you don't ignore or exclude it explicitly as in --exclude mdns. For Webpack you can declare it as an external.

Usage

var SignalKClient = require('@signalk/signalk-client').Client;
var signalk = new SignalKClient;
var connection;

var thisCallback = function(msg) {
  $.each(listenerList, function(i, obj) {
    obj.onmessage(msg, connection);
  });
};

function connectDelta(host, thisCallback, onConnect, onDisconnect) {
  debug("Connecting to " + host);

  // try mdns
  connection = signalk.discoverAndConnect();
  if(connection) {
    return;
  }

  console.log("Could not use mdns, falling back to " + host);

  connection = signalk.connectDelta(host, thisCallback,
    function(skConnection) {
      skConnection.subscribeAll();
      onConnect();
    },

    function(skConnection) {
      skConnection.close();
      debug('Disconnected');
    },

    function(error) {
      console.log(error)
    },

    'self'
  );
}