fix(core): update

This commit is contained in:
2019-04-23 00:28:57 +02:00
parent a8a91b4db2
commit b1017121ea
10 changed files with 112 additions and 180 deletions

View File

@ -12,4 +12,4 @@ export interface IServerPutMessageRequestBody {
passphrase: string;
message: string;
payload: any;
}
}

View File

@ -7,10 +7,7 @@ import * as url from 'url';
import * as interfaces from './interfaces';
import {
ClientUniverseChannel,
UniverseMessage
} from './';
import { ClientUniverseChannel, UniverseMessage } from './';
export interface IClientOptions {
serverAddress: string;
@ -35,47 +32,46 @@ export class ClientUniverse {
* adds a channel to the channelcache
* TODO: verify channel before adding it to the channel cache
*/
public async addChannel (channelNameArg: string, passphraseArg: string) {
const existingChannel = this.getChannel(channelNameArg);
public async addChannel(channelNameArg: string, passphraseArg: string) {
const existingChannel = await this.getChannel(channelNameArg);
if (existingChannel) {
throw new Error('channel exists');
}
// lets create the channel
ClientUniverseChannel.createClientUniverseChannel(
this,
channelNameArg,
passphraseArg
);
ClientUniverseChannel.createClientUniverseChannel(this, channelNameArg, passphraseArg);
}
/**
* gets a channel from the channelcache
* @param channelName
* @param passphraseArg
* @param passphraseArg
*/
public async getChannel(channelName: string): Promise<ClientUniverseChannel> {
await this.checkConnection();
const clientUniverseChannel = this.channelCache.find(channel => {
return channel.name === channelName;
})
});
return clientUniverseChannel;
}
public async sendMessage(messageArg: interfaces.IMessageCreator) {
console.log('hello');
const requestBody: interfaces.IUniverseMessage = {
id: plugins.smartunique.shortId(),
timestamp: Date.now(),
passphrase: (await this.getChannel(messageArg.targetChannelName)).passphrase,
...messageArg,
...messageArg
};
const requestBodyString = JSON.stringify(requestBody);
// TODO: User websocket connection if available
const response = await plugins.smartrequest.postJson(`${this.options.serverAddress}/sendmessage` , {
requestBody: requestBodyString
});
const response = await plugins.smartrequest.postJson(
`${this.options.serverAddress}/sendmessage`,
{
requestBody: requestBodyString
}
);
}
public close() {

View File

@ -10,15 +10,19 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel {
/**
* creates a channel and adds it to the cache of clientUniverseArg
* @param clientUniverseArg
* @param channelNameArg
* @param passphraseArg
* @param channelNameArg
* @param passphraseArg
*/
public static async createClientUniverseChannel(
clientUniverseArg: ClientUniverse,
channelNameArg: string,
passphraseArg: string
): Promise<ClientUniverseChannel> {
const clientChannel = new ClientUniverseChannel(clientUniverseArg, channelNameArg, passphraseArg);
const clientChannel = new ClientUniverseChannel(
clientUniverseArg,
channelNameArg,
passphraseArg
);
clientUniverseArg.channelCache.add(clientChannel);
await clientChannel.subscribe();
return clientChannel;
@ -31,11 +35,11 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel {
// properties
public name: string;
public passphrase: string;
// refs
public clientUniverse: ClientUniverse;
constructor(clientUniverseArg: ClientUniverse, nameArg: string, passphraseArg: string) {
constructor(clientUniverseArg: ClientUniverse, nameArg: string, passphraseArg: string) {
this.clientUniverse = clientUniverseArg;
this.name = nameArg;
this.passphrase = passphraseArg;

View File

@ -6,9 +6,7 @@ export class ClientUniverseMessage implements interfaces.IUniverseMessage {
// ======
// STATIC
// ======
public static createMessageFromPayload(messageDescriptor: interfaces.IUniverseMessage) {
};
public static createMessageFromPayload(messageDescriptor: interfaces.IUniverseMessage) {}
// ========
// INSTANCE
@ -26,7 +24,5 @@ export class ClientUniverseMessage implements interfaces.IUniverseMessage {
public targetChannelName: string;
constructor(messageArg, payloadArg) {}
getAsJsonForPayload () {
}
getAsJsonForPayload() {}
}

View File

@ -11,8 +11,6 @@ export interface ISmartUniverseConstructorOptions {
messageExpiryInMilliseconds: number;
}
/**
* main class that setsup a Universe
*/
@ -67,7 +65,7 @@ export class Universe {
/**
* initiates a server
*/
public async start(portArg: number | string) {
public async start(portArg: number) {
// lets create the base smartexpress server
this.smartexpressServer = new plugins.smartexpress.Server({
cors: true,
@ -79,10 +77,15 @@ export class Universe {
});
// lets create the http request route
this.smartexpressServer.addRoute('/sendmessage', new Handler('POST', async (req, res) => {
const universeMessageInstance = new UniverseMessage(req.body);
this.universeCache.addMessage(universeMessageInstance);
}));
this.smartexpressServer.addRoute(
'/sendmessage',
new Handler('POST', async (req, res) => {
const universeMessageInstance = new UniverseMessage(req.body);
this.universeCache.addMessage(universeMessageInstance);
res.status(200);
res.end();
})
);
// add websocket upgrade
this.smartsocket = new plugins.smartsocket.Smartsocket({

View File

@ -14,7 +14,6 @@ import { IUniverseMessage } from './interfaces';
* acts as a container to save message states like authentication status
*/
export class UniverseMessage implements interfaces.IUniverseMessage {
public id: string;
public timestamp: number;
@ -40,7 +39,7 @@ export class UniverseMessage implements interfaces.IUniverseMessage {
* wether the message is authenticated
*/
public authenticated: boolean = null;
/**
* a destruction timer for this message
*/