fix(core): update

This commit is contained in:
Philipp Kunz 2019-04-22 13:06:01 +02:00
parent 83807d7c5c
commit a5e849aa17
4 changed files with 38 additions and 20 deletions

View File

@ -11,7 +11,7 @@ let testClientChannel: smartuniverse.ClientUniverseChannel;
const testChannelData = { const testChannelData = {
channelName: 'awesomeTestChannel', channelName: 'awesomeTestChannel',
channelPass: 'awesomeChannelPAss' channelPass: 'awesomeChannelPAss'
} };
tap.test('first test', async () => { tap.test('first test', async () => {
testUniverse = new smartuniverse.Universe({ testUniverse = new smartuniverse.Universe({
@ -43,9 +43,7 @@ tap.test('should get a observable correctly', async () => {
tap.test('should send a message correctly', async () => { tap.test('should send a message correctly', async () => {
await testUniverseClient.sendMessage({ await testUniverseClient.sendMessage({
messageText: 'hello', messageText: 'hello',
passphrase: 'wowza', targetChannelName: 'channel1'
targetChannelName: 'channel1',
}); });
}); });

View File

@ -31,11 +31,34 @@ export class ClientUniverse {
this.options = optionsArg; 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<ClientUniverseChannel> {
await this.checkConnection();
return clientUniverseChannel;
}
public async sendMessage(messageArg: interfaces.IMessageCreator) { public async sendMessage(messageArg: interfaces.IMessageCreator) {
const requestBody: interfaces.IUniverseMessage = { const requestBody: interfaces.IUniverseMessage = {
id: plugins.smartunique.shortId(), id: plugins.smartunique.shortId(),
timestamp: Date.now(), timestamp: Date.now(),
passphrase: (await this.getChannel(messageArg.targetChannelName))., passphrase: (await this.getChannel(messageArg.targetChannelName)).passphrase,
...messageArg, ...messageArg,
}; };
@ -46,21 +69,15 @@ export class ClientUniverse {
}); });
} }
public async getChannel(channelName: string, passphraseArg?: string): Promise<ClientUniverseChannel> {
await this.checkConnection();
const clientUniverseChannel = await ClientUniverseChannel.createClientUniverseChannel(
this,
channelName
);
this.channelCache.add(clientUniverseChannel);
return clientUniverseChannel;
}
public close() { public close() {
this.socketClient.disconnect(); 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<void> {
if (!this.socketClient && !this.observableIntake) { if (!this.socketClient && !this.observableIntake) {
const parsedURL = url.parse(this.options.serverAddress); const parsedURL = url.parse(this.options.serverAddress);
this.socketClient = new SmartsocketClient({ this.socketClient = new SmartsocketClient({

View File

@ -13,7 +13,7 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel {
passphraseArg: string passphraseArg: string
): Promise<ClientUniverseChannel> { ): Promise<ClientUniverseChannel> {
const clientChannel = new ClientUniverseChannel(clientUniverseArg, passphraseArg); const clientChannel = new ClientUniverseChannel(clientUniverseArg, passphraseArg);
await clientChannel.transmitSubscription(); await clientChannel.subscribe();
return clientChannel; return clientChannel;
} }
@ -26,13 +26,14 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel {
constructor(clientUniverseArg: ClientUniverse, passphraseArg: string) { constructor(clientUniverseArg: ClientUniverse, passphraseArg: string) {
this.clientUniverse = clientUniverseArg; this.clientUniverse = clientUniverseArg;
this.passphrase = passphraseArg this.passphrase = passphraseArg;
} }
/** /**
* subscribes to a channel
* tells the universe about this instances interest into a channel * tells the universe about this instances interest into a channel
*/ */
public async transmitSubscription() { public async subscribe() {
this.clientUniverse.socketClient; this.clientUniverse.socketClient;
} }
} }

View File

@ -101,7 +101,9 @@ export class Universe {
const SubscriptionSocketFunction = new plugins.smartsocket.SocketFunction({ const SubscriptionSocketFunction = new plugins.smartsocket.SocketFunction({
allowedRoles: [ClientRole], allowedRoles: [ClientRole],
funcName: 'channelSubscription', 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 // add smartsocket to the running smartexpress app