UNPKG

1.86 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 MSearch_1 = require("./MSearch");
7const SocketBase_1 = require("./SocketBase");
8/**
9 * Class representing a SSDP socket that support the HTTP method M-SEARCH.
10 */
11class MSearchSocket extends SocketBase_1.SocketBase {
12 /**
13 * @param address The network address to listen for M-SEARCH responses on.
14 */
15 constructor(address) {
16 super();
17 this.address = address;
18 }
19 /**
20 * Starts a search by using HTTP method M-SEARCH.
21 */
22 search() {
23 logging_1.log('MSearchSocket#search - %s', this.address);
24 const message = new MSearch_1.MSearch().toBuffer();
25 return new Promise((resolve, reject) => {
26 this.socket.send(message, 0, message.length, Constants_1.SSDP_PORT, Constants_1.SSDP_MULTICAST_ADDRESS, (error) => {
27 if (error) {
28 logging_1.log('MSearchSocket#search - %o', error);
29 reject(error);
30 }
31 else {
32 resolve();
33 }
34 });
35 });
36 }
37 onListening() {
38 logging_1.log('MSearchSocket#onListening - %s:%d', this.socket.address().address, this.socket.address().port);
39 }
40 onMessage(messageBuffer, remote) {
41 const message = new Message_1.Message(remote.address, messageBuffer);
42 if (message.method !== 'HTTP/1.1 200 OK') {
43 return;
44 }
45 this.emit('hello', message);
46 }
47 bind() {
48 return new Promise((resove) => {
49 this.socket.bind(undefined, this.address, () => resove());
50 });
51 }
52}
53exports.MSearchSocket = MSearchSocket;
54//# sourceMappingURL=MSearchSocket.js.map
\No newline at end of file