fix(core): update

This commit is contained in:
2019-04-24 18:20:31 +02:00
parent d43ad80784
commit af9f590349
8 changed files with 72 additions and 53 deletions

View File

@ -57,19 +57,18 @@ export class ClientUniverse {
}
public async sendMessage(messageArg: interfaces.IMessageCreator) {
console.log('hello');
await this.checkConnection();
const requestBody: interfaces.IUniverseMessage = {
id: plugins.smartunique.shortId(),
timestamp: Date.now(),
passphrase: (await this.getChannel(messageArg.targetChannelName)).passphrase,
...messageArg
};
const requestBodyString = JSON.stringify(requestBody);
// TODO: User websocket connection if available
const response = await plugins.smartrequest.postJson(
`${this.options.serverAddress}/sendmessage`,
{
requestBody: requestBodyString
requestBody
}
);
}
@ -85,15 +84,17 @@ export class ClientUniverse {
private async checkConnection(): Promise<void> {
if (!this.socketClient && !this.observableIntake) {
const parsedURL = url.parse(this.options.serverAddress);
this.socketClient = new SmartsocketClient({
const socketConfig: plugins.smartsocket.ISmartsocketClientOptions = {
alias: process.env.SOCKET_ALIAS || 'someclient',
password: 'UniverseClient',
port: parseInt(parsedURL.port, 10),
role: 'UniverseClient',
url: parsedURL.hostname
});
url: parsedURL.protocol + '//' + parsedURL.hostname
};
console.log(socketConfig);
this.socketClient = new SmartsocketClient(socketConfig);
this.observableIntake = new plugins.smartrx.ObservableIntake();
this.socketClient.connect();
await this.socketClient.connect();
}
}
}

View File

@ -80,22 +80,21 @@ export class Universe {
this.smartexpressServer.addRoute(
'/sendmessage',
new Handler('POST', async (req, res) => {
const universeMessageInstance = new UniverseMessage(req.body);
const universeMessageInstance: UniverseMessage = new UniverseMessage(req.body);
this.universeCache.addMessage(universeMessageInstance);
res.status(200);
res.end();
})
);
// add websocket upgrade
this.smartsocket = new plugins.smartsocket.Smartsocket({
port: 12345 // fix this within smartsocket
});
this.smartsocket = new plugins.smartsocket.Smartsocket({});
// add a role for the clients
const ClientRole = new plugins.smartsocket.SocketRole({
name: 'clientuniverse',
passwordHash: 'clientuniverse' // authentication happens on another level
name: 'UniverseClient',
passwordHash: plugins.smarthash.sha256FromStringSync('UniverseClient') // authentication happens on another level
});
// add the role to smartsocket
@ -105,18 +104,16 @@ export class Universe {
allowedRoles: [ClientRole],
funcName: 'channelSubscription',
funcDef: () => {
console.log('a client connected');
} // TODO: implement an action upon connection of clients
});
// add smartsocket to the running smartexpress app
this.smartsocket.setExternalServer('express', this.smartexpressServer as any);
// start the socket
this.smartsocket.start();
// start the smartexpress instance
this.smartsocket.setExternalServer('smartexpress', this.smartexpressServer as any);
// start everything
await this.smartexpressServer.start();
await this.smartsocket.start();
console.log('started universe');
}
/**

View File

@ -52,7 +52,7 @@ export class UniverseCache {
/**
* Read a message from the UniverseCache
*/
public readMessagesYoungerThan(unixTimeArg?: number): Observable<UniverseMessage> {
public readMessagesYoungerThan(unixTimeArg?: number, channelName?: string): Observable<UniverseMessage> {
const messageObservable = from(this.messageMap.getArray()).pipe(
filter(messageArg => {
return messageArg.smartTimestamp.isYoungerThanMilliSeconds(this.destructionTime);

View File

@ -88,5 +88,7 @@ export class UniverseChannel {
);
}
public pushToClients(messageArg: UniverseMessage) {}
public pushToClients(messageArg: UniverseMessage) {
}
}

View File

@ -0,0 +1,20 @@
import * as plugins from './smartuniverse.plugins';
import { UniverseChannel } from './smartuniverse.classes.universechannel';
/**
* represents a subscription into a specific topic
*/
export class UniverseConnection {
/**
* the socketClient to ping
*/
socketclient: plugins.smartsocket.SmartsocketClient;
subscribedChannels: UniverseChannel[] = [];
authenticatedChannels: UniverseChannel[] = [];
constructor() {
}
}