diff --git a/src/server.ts b/src/server.ts index 7de2139..d911a99 100644 --- a/src/server.ts +++ b/src/server.ts @@ -65,10 +65,14 @@ export class MultipleThings { * @param {String} name The mDNS server name */ constructor(things: Thing[], name: string) { - this.things = things; + this.things = things.slice(); this.name = name; } + addThing(thing: Thing): number { + return this.things.push(thing) - 1; + } + /** * Get the thing at the given index. * @@ -848,6 +852,14 @@ export class WebThingServer { this.server.on('request', this.app); } + addThing(thing: Thing): void { + if (this.things instanceof SingleThing) { + throw new Error('Cannot add new things when Webthings was instanciated with a SingleThing.'); + } + const i = this.things.addThing(thing); + thing.setHrefPrefix(`${this.basePath}/${i}`); + } + /** * Start listening for incoming connections. *