diff --git a/test/test.ts b/test/test.ts index c3585b3..e7abea3 100644 --- a/test/test.ts +++ b/test/test.ts @@ -50,9 +50,8 @@ tap.test('should get a observable correctly', async () => { }); tap.test('should send a message correctly', async () => { - await testClientUniverse.sendMessage({ - messageText: 'hello', - targetChannelName: testChannelData.channelName + await (await testClientUniverse.getChannel(testChannelData.channelName)).sendMessage({ + messageText: 'hello' }); }); diff --git a/ts/interfaces/universemessage.interfaces.ts b/ts/interfaces/universemessage.interfaces.ts index d909e63..78eaf07 100644 --- a/ts/interfaces/universemessage.interfaces.ts +++ b/ts/interfaces/universemessage.interfaces.ts @@ -2,9 +2,11 @@ export interface IMessageCreator { messageText: string; payload?: string | number | any; payloadStringType?: 'Buffer' | 'string' | 'object'; - targetChannelName: string; } +/** + * + */ export interface IUniverseMessage extends IMessageCreator { id: string; /** @@ -12,4 +14,5 @@ export interface IUniverseMessage extends IMessageCreator { */ timestamp: number; passphrase: string; + targetChannelName: string; } diff --git a/ts/smartuniverse.classes.clientuniverse.ts b/ts/smartuniverse.classes.clientuniverse.ts index 2e40984..3f7b6b1 100644 --- a/ts/smartuniverse.classes.clientuniverse.ts +++ b/ts/smartuniverse.classes.clientuniverse.ts @@ -42,7 +42,7 @@ export class ClientUniverse { } // lets create the channel - ClientUniverseChannel.createClientUniverseChannel(this, channelNameArg, passphraseArg); + await ClientUniverseChannel.createClientUniverseChannel(this, channelNameArg, passphraseArg); } /** @@ -68,21 +68,6 @@ export class ClientUniverse { }); } - /** - * sends a message towards the server - * @param messageArg - */ - public async sendMessage(messageArg: interfaces.IMessageCreator) { - await this.checkConnection(); - const universeMessageToSend: interfaces.IUniverseMessage = { - id: plugins.smartunique.shortId(), - timestamp: Date.now(), - passphrase: (await this.getChannel(messageArg.targetChannelName)).passphrase, - ...messageArg - }; - await this.smartsocketClient.serverCall('processMessage', universeMessageToSend); - } - public close() { this.smartsocketClient.disconnect(); } @@ -91,7 +76,7 @@ export class ClientUniverse { * 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 { + public async checkConnection(): Promise { if (!this.smartsocketClient && !this.observableIntake) { const parsedURL = url.parse(this.options.serverAddress); const socketConfig: plugins.smartsocket.ISmartsocketClientOptions = { diff --git a/ts/smartuniverse.classes.clientuniversechannel.ts b/ts/smartuniverse.classes.clientuniversechannel.ts index 938994e..2608998 100644 --- a/ts/smartuniverse.classes.clientuniversechannel.ts +++ b/ts/smartuniverse.classes.clientuniversechannel.ts @@ -37,10 +37,10 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel { public passphrase: string; // refs - public clientUniverse: ClientUniverse; + public clientUniverseRef: ClientUniverse; constructor(clientUniverseArg: ClientUniverse, nameArg: string, passphraseArg: string) { - this.clientUniverse = clientUniverseArg; + this.clientUniverseRef = clientUniverseArg; this.name = nameArg; this.passphrase = passphraseArg; } @@ -55,6 +55,24 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel { name: this.name, passphrase: this.passphrase }; - this.clientUniverse.smartsocketClient.serverCall(serverCallActionName, serverCallActionPayload); + await this.clientUniverseRef.smartsocketClient.serverCall(serverCallActionName, serverCallActionPayload); + } + + /** + * sends a message towards the server + * @param messageArg + */ + public async sendMessage(messageArg: interfaces.IMessageCreator) { + await this.clientUniverseRef.checkConnection(); + const universeMessageToSend: interfaces.IUniverseMessage = { + id: plugins.smartunique.shortId(), + timestamp: Date.now(), + passphrase: this.passphrase, + targetChannelName: this.name, + messageText: messageArg.messageText, + payload: messageArg.payload, + payloadStringType: messageArg.payloadStringType + }; + await this.clientUniverseRef.smartsocketClient.serverCall('processMessage', universeMessageToSend); } } diff --git a/ts/smartuniverse.classes.universe.ts b/ts/smartuniverse.classes.universe.ts index e6bea5e..c9bf471 100644 --- a/ts/smartuniverse.classes.universe.ts +++ b/ts/smartuniverse.classes.universe.ts @@ -124,16 +124,9 @@ export class Universe { if (universeConnection) { console.log('found UniverseConnection for socket'); } else { - console.log('universe client not yet present'); - console.log('creating one now as send only'); - const universeConnectionInstance = new UniverseConnection({ - socketConnection: socketConnectionArg, - authenticationRequests: [] - }); - await UniverseConnection.addConnectionToCache( - this.universeCache, - universeConnectionInstance - ); + return { + error: 'You need to authenticate for a channel' + }; } const unauthenticatedMessage = UniverseMessage.createMessageFromPayload(dataArg); const foundChannel = await UniverseChannel.authorizeAMessageForAChannel( diff --git a/ts/smartuniverse.classes.universeconnection.ts b/ts/smartuniverse.classes.universeconnection.ts index 009636f..bc827ca 100644 --- a/ts/smartuniverse.classes.universeconnection.ts +++ b/ts/smartuniverse.classes.universeconnection.ts @@ -23,6 +23,7 @@ export class UniverseConnection { universeConnection = await UniverseConnection.authenticateAuthenticationRequests( universeConnection ); + universeCache.connectionMap.add(universeConnection); } /**