UNPKG

1.81 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const logging_1 = require("../logging");
4const Constants_1 = require("./Constants");
5const Message_1 = require("./Message");
6const SocketBase_1 = require("./SocketBase");
7/**
8 * Class representing a SSDP socket that support the HTTP method NOTIFY.
9 */
10class NotifySocket extends SocketBase_1.SocketBase {
11 /**
12 * @param addresses The network addresses to listen for NOTIFY
13 * advertisements on.
14 */
15 constructor(addresses) {
16 super();
17 this.addresses = addresses;
18 }
19 onListening() {
20 logging_1.log('NotifySocket#onListening - %s:%d', this.socket.address().address, this.socket.address().port);
21 for (const address of this.addresses) {
22 logging_1.log('NotifySocket#onListening - add membership to %s', address);
23 try {
24 this.socket.addMembership(Constants_1.SSDP_MULTICAST_ADDRESS, address);
25 }
26 catch (error) {
27 logging_1.log('NotifySocket#onListening - %o', error);
28 }
29 }
30 }
31 onMessage(messageBuffer, remote) {
32 const message = new Message_1.Message(remote.address, messageBuffer);
33 if (message.method !== 'NOTIFY * HTTP/1.1' ||
34 message.nt !== 'urn:axis-com:service:BasicService:1') {
35 return;
36 }
37 if (message.nts === 'ssdp:alive') {
38 this.emit('hello', message);
39 }
40 else if (message.nts === 'ssdp:byebye') {
41 this.emit('goodbye', message);
42 }
43 }
44 bind() {
45 return new Promise((resolve) => {
46 this.socket.bind(Constants_1.SSDP_PORT, undefined, () => resolve());
47 });
48 }
49}
50exports.NotifySocket = NotifySocket;
51//# sourceMappingURL=NotifySocket.js.map
\No newline at end of file