diff --git a/test/test.ts b/test/test.ts index c6e553e..1305e90 100644 --- a/test/test.ts +++ b/test/test.ts @@ -11,7 +11,7 @@ let testClientChannel: smartuniverse.ClientUniverseChannel; const testChannelData = { channelName: 'awesomeTestChannel', channelPass: 'awesomeChannelPAss' -} +}; tap.test('first test', async () => { testUniverse = new smartuniverse.Universe({ @@ -43,9 +43,7 @@ tap.test('should get a observable correctly', async () => { tap.test('should send a message correctly', async () => { await testUniverseClient.sendMessage({ messageText: 'hello', - passphrase: 'wowza', - targetChannelName: 'channel1', - + targetChannelName: 'channel1' }); }); diff --git a/ts/smartuniverse.classes.clientuniverse.ts b/ts/smartuniverse.classes.clientuniverse.ts index c74927c..e24b442 100644 --- a/ts/smartuniverse.classes.clientuniverse.ts +++ b/ts/smartuniverse.classes.clientuniverse.ts @@ -31,11 +31,34 @@ export class ClientUniverse { this.options = optionsArg; } + /** + * adds a channel to the channelcache + * TODO: verify channel before adding it to the channel cache + */ + public async addChannel (channelNameArg: string, passphraseArg: string) { + const clientUniverseChannel = await ClientUniverseChannel.createClientUniverseChannel( + this, + channelNameArg, + passphraseArg + ); + this.channelCache.add(clientUniverseChannel); + } + + /** + * gets a channel from the channelcache + * @param channelName + * @param passphraseArg + */ + public async getChannel(channelName: string, passphraseArg?: string): Promise { + await this.checkConnection(); + return clientUniverseChannel; + } + public async sendMessage(messageArg: interfaces.IMessageCreator) { const requestBody: interfaces.IUniverseMessage = { id: plugins.smartunique.shortId(), timestamp: Date.now(), - passphrase: (await this.getChannel(messageArg.targetChannelName))., + passphrase: (await this.getChannel(messageArg.targetChannelName)).passphrase, ...messageArg, }; @@ -46,21 +69,15 @@ export class ClientUniverse { }); } - public async getChannel(channelName: string, passphraseArg?: string): Promise { - await this.checkConnection(); - const clientUniverseChannel = await ClientUniverseChannel.createClientUniverseChannel( - this, - channelName - ); - this.channelCache.add(clientUniverseChannel); - return clientUniverseChannel; - } - public close() { this.socketClient.disconnect(); } - private async checkConnection() { + /** + * checks the connection towards a universe server + * since password validation is done through other means, a connection should always be possible + */ + private async checkConnection(): Promise { if (!this.socketClient && !this.observableIntake) { const parsedURL = url.parse(this.options.serverAddress); this.socketClient = new SmartsocketClient({ diff --git a/ts/smartuniverse.classes.clientuniversechannel.ts b/ts/smartuniverse.classes.clientuniversechannel.ts index 5ddf9d2..8c344b6 100644 --- a/ts/smartuniverse.classes.clientuniversechannel.ts +++ b/ts/smartuniverse.classes.clientuniversechannel.ts @@ -13,7 +13,7 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel { passphraseArg: string ): Promise { const clientChannel = new ClientUniverseChannel(clientUniverseArg, passphraseArg); - await clientChannel.transmitSubscription(); + await clientChannel.subscribe(); return clientChannel; } @@ -26,13 +26,14 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel { constructor(clientUniverseArg: ClientUniverse, passphraseArg: string) { this.clientUniverse = clientUniverseArg; - this.passphrase = passphraseArg + this.passphrase = passphraseArg; } /** + * subscribes to a channel * tells the universe about this instances interest into a channel */ - public async transmitSubscription() { + public async subscribe() { this.clientUniverse.socketClient; } } diff --git a/ts/smartuniverse.classes.universe.ts b/ts/smartuniverse.classes.universe.ts index 3434afd..e6c3287 100644 --- a/ts/smartuniverse.classes.universe.ts +++ b/ts/smartuniverse.classes.universe.ts @@ -101,7 +101,9 @@ export class Universe { const SubscriptionSocketFunction = new plugins.smartsocket.SocketFunction({ allowedRoles: [ClientRole], funcName: 'channelSubscription', - funcDef: () => {} // TODO: implement an action upon connection of clients + funcDef: () => { + console.log('a client connected'); + } // TODO: implement an action upon connection of clients }); // add smartsocket to the running smartexpress app