Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
[![Coverage Status](https://img.shields.io/coveralls/diversario/node-ssdp.svg)](https://coveralls.io/r/diversario/node-ssdp?branch=master)
[![Dependency Status](https://gemnasium.com/diversario/node-ssdp.png)](https://gemnasium.com/diversario/node-ssdp)
[![NPM version](https://badge.fury.io/js/node-ssdp.svg)](http://badge.fury.io/js/node-ssdp)
[![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg)](https://github.com/standard/semistandard)

## Installation

Expand Down
22 changes: 11 additions & 11 deletions example/client.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
var ssdp = require('../index').Client
, client = new ssdp({})
const { Client } = require('../index');
const client = new Client({});

client.on('notify', function () {
//console.log('Got a notification.')
})
// console.log('Got a notification.')
});

client.on('response', function inResponse(headers, code, rinfo) {
console.log('Got a response to an m-search:\n%d\n%s\n%s', code, JSON.stringify(headers, null, ' '), JSON.stringify(rinfo, null, ' '))
})
client.on('response', function inResponse (headers, code, rinfo) {
console.log('Got a response to an m-search:\n%d\n%s\n%s', code, JSON.stringify(headers, null, ' '), JSON.stringify(rinfo, null, ' '));
});

client.search('urn:schemas-upnp-org:service:ContentDirectory:1')
client.search('urn:schemas-upnp-org:service:ContentDirectory:1');

// Or maybe if you want to scour for everything after 5 seconds
setInterval(function() {
client.search('ssdp:all')
}, 5000)
setInterval(function () {
client.search('ssdp:all');
}, 5000);

// And after 10 seconds, you want to stop
// setTimeout(function () {
Expand Down
36 changes: 18 additions & 18 deletions example/server.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
var SSDP = require('../index').Server
, server = new SSDP({
location: {
port: 8080,
path: '/ssdp/device-desc.xml'
}
})
const { Server } = require('../index');
const server = new Server({
location: {
port: 8080,
path: '/ssdp/device-desc.xml'
}
});

server.addUSN('upnp:rootdevice')
server.addUSN('urn:schemas-upnp-org:device:MediaServer:1')
server.addUSN('urn:schemas-upnp-org:service:ContentDirectory:1')
server.addUSN('urn:schemas-upnp-org:service:ConnectionManager:1')
server.addUSN('upnp:rootdevice');
server.addUSN('urn:schemas-upnp-org:device:MediaServer:1');
server.addUSN('urn:schemas-upnp-org:service:ContentDirectory:1');
server.addUSN('urn:schemas-upnp-org:service:ConnectionManager:1');

server.on('advertise-alive', function (heads) {
//console.log('advertise-alive', heads)
// console.log('advertise-alive', heads)
// Expire old devices from your cache.
// Register advertising device somewhere (as designated in http headers heads)
})
});

server.on('advertise-bye', function (heads) {
//console.log('advertise-bye', heads)
// console.log('advertise-bye', heads)
// Remove specified device from cache.
})
});

// start server on all interfaces
server.start()
.catch(e => {
console.log('Failed to start server:', e)
console.log('Failed to start server:', e);
})
.then(() => {
console.log('Server started.')
})
console.log('Server started.');
});
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SOFTWARE.
*/

module.exports = {
Server: require("./lib/server"),
Client: require("./lib/client"),
Base: require("./lib/index")
}
Server: require('./lib/server'),
Client: require('./lib/client'),
Base: require('./lib/index')
};
132 changes: 59 additions & 73 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'
'use strict';

/*
MIT License
Expand All @@ -24,84 +24,70 @@
SOFTWARE.
*/

var SSDP = require('./')
, util = require('util')
, c = require('./const')
, Promise = require('bluebird')
, SsdpHeader = require('./ssdpHeader')

/**
*
* @param opts
* @constructor
*/
function SsdpClient(opts) {
this._subclass = 'node-ssdp:client'
SSDP.call(this, opts)
}

const SSDP = require('./');
const c = require('./const');
const SsdpHeader = require('./ssdpHeader');

class SsdpClient extends SSDP {
constructor (opts) {
super(opts);
this._subclass = 'node-ssdp:client';
}

util.inherits(SsdpClient, SSDP)
/**
* Start the listener for multicast notifications from SSDP devices
* @param [cb]
*/
start (cb) {
return new Promise((resolve, reject) => {
this._start((err, ...args) => {
if (cb) {
cb(err, ...args);
}
if (err) {
return reject(err);
}
resolve();
});
});
}

/**
* Close UDP socket.
*/
stop () {
this._stop();
}

/**
* Start the listener for multicast notifications from SSDP devices
* @param [cb]
*/
SsdpClient.prototype.start = function (cb) {
var self = this;
return new Promise(function(success, failure) {
function onBind(err) {
if (cb) cb.apply(self, arguments)
if (err) return failure(err)
success()
/**
* Search for a given service type.
* @param {String} serviceType
* @returns {*}
*/
search (serviceType) {
if (!this._started) {
return this.start(() => {
this.search(serviceType);
});
}
self._start(onBind)
})
}


/**
*Close UDP socket.
*/
SsdpClient.prototype.stop = function () {
this._stop()
}


/**
*
* @param {String} serviceType
* @returns {*}
*/
SsdpClient.prototype.search = function search(serviceType) {
var self = this

if (!this._started) {
return this.start(function () {
self.search(serviceType)
})
const header = new SsdpHeader(c.M_SEARCH, {
HOST: this._ssdpServerHost,
ST: serviceType,
MAN: '"ssdp:discover"',
MX: 3
});

this._logger('Attempting to send an M-SEARCH request');

this._send(header, (err, bytes) => {
if (err) {
this._logger('Error: unable to send M-SEARCH request ID %s: %o', header.id(), err);
} else {
this._logger('Sent M-SEARCH request: %o', { message: header.toString(), id: header.id() });
}
});
}

var header = new SsdpHeader(c.M_SEARCH, {
'HOST': self._ssdpServerHost,
'ST': serviceType,
'MAN': '"ssdp:discover"',
'MX': 3
})

self._logger('Attempting to send an M-SEARCH request')

self._send(header, function (err, bytes) {
if (err) {
self._logger('Error: unable to send M-SEARCH request ID %s: %o', header.id(), err)
} else {
self._logger('Sent M-SEARCH request: %o', {'message': header.toString(), id: header.id()})
}
})
}



module.exports = SsdpClient
module.exports = SsdpClient;
4 changes: 2 additions & 2 deletions lib/const.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'
'use strict';

/*
MIT License
Expand Down Expand Up @@ -34,4 +34,4 @@ module.exports = {
M_SEARCH: 'm-search',
SSDP_DEFAULT_IP: '239.255.255.250',
SSDP_DEFAULT_PORT: 1900
}
};
Loading