UNPKG

axis-discovery-ssdp

Version:

A Node.js SSDP (UPnP) client library written in TypeScript capable of searching for Axis Communication cameras.

58 lines 2.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NotifySocket = void 0; const logging_1 = require("../logging"); const Constants_1 = require("./Constants"); const Message_1 = require("./Message"); const SocketBase_1 = require("./SocketBase"); /** * Class representing a SSDP socket that support the HTTP method NOTIFY. */ class NotifySocket extends SocketBase_1.SocketBase { /** * Initializes a new instance of the class. * @param addresses The network addresses to listen for NOTIFY * advertisements on. */ constructor(addresses) { super(); this.addresses = addresses; } onListening() { if (!this.socket) { throw new Error('Notify socket has never been started'); } (0, logging_1.log)('NotifySocket#onListening - %s:%d', this.socket.address().address, this.socket.address().port); for (const address of this.addresses) { (0, logging_1.log)('NotifySocket#onListening - add membership to %s', address); try { this.socket.addMembership(Constants_1.SSDP_MULTICAST_ADDRESS, address); } catch (error) { (0, logging_1.log)('NotifySocket#onListening - %o', error); } } } onMessage(messageBuffer, remote) { const message = new Message_1.Message(remote.address, messageBuffer); if (message.method !== 'NOTIFY * HTTP/1.1' || message.nt !== 'urn:axis-com:service:BasicService:1') { return; } if (message.nts === 'ssdp:alive') { this.emit('hello', message); } else if (message.nts === 'ssdp:byebye') { this.emit('goodbye', message); } } bind() { return new Promise((resolve) => { if (!this.socket) { throw new Error('Notify socket has never been started'); } this.socket.bind(Constants_1.SSDP_PORT, undefined, () => resolve()); }); } } exports.NotifySocket = NotifySocket; //# sourceMappingURL=NotifySocket.js.map