smartuniverse/ts/smartuniverse.classes.clientuniverse.ts

122 lines
3.5 KiB
TypeScript
Raw Normal View History

2018-03-13 05:15:40 +00:00
import * as plugins from './smartuniverse.plugins';
2019-01-31 01:52:18 +00:00
import { Objectmap } from '@pushrocks/lik';
2018-03-13 05:15:40 +00:00
import { Observable } from 'rxjs';
2019-01-31 01:52:18 +00:00
import { Smartsocket, SmartsocketClient } from '@pushrocks/smartsocket';
2018-03-20 07:16:54 +00:00
import * as url from 'url';
2019-04-11 15:52:01 +00:00
import * as interfaces from './interfaces';
2019-04-22 22:28:57 +00:00
import { ClientUniverseChannel, UniverseMessage } from './';
2018-03-13 05:15:40 +00:00
2018-03-15 00:05:13 +00:00
export interface IClientOptions {
2018-03-20 07:16:54 +00:00
serverAddress: string;
2018-03-15 00:05:13 +00:00
}
2018-04-13 13:45:48 +00:00
/**
* this class is for client side only!!!
* allows connecting to a universe server
*/
export class ClientUniverse {
2018-03-15 00:05:13 +00:00
public options;
2019-04-24 21:27:57 +00:00
public smartsocketClient: plugins.smartsocket.SmartsocketClient;
public observableIntake: plugins.smartrx.ObservableIntake<UniverseMessage>;
public channelCache = new Objectmap<ClientUniverseChannel>();
2018-03-13 05:15:40 +00:00
2018-03-15 00:05:13 +00:00
constructor(optionsArg: IClientOptions) {
this.options = optionsArg;
}
2018-03-20 07:16:54 +00:00
2019-04-22 11:06:01 +00:00
/**
* adds a channel to the channelcache
* TODO: verify channel before adding it to the channel cache
*/
2019-04-22 22:28:57 +00:00
public async addChannel(channelNameArg: string, passphraseArg: string) {
const existingChannel = await this.getChannel(channelNameArg);
2019-04-22 20:04:52 +00:00
if (existingChannel) {
throw new Error('channel exists');
}
2019-04-22 21:11:51 +00:00
// lets create the channel
2019-04-22 22:28:57 +00:00
ClientUniverseChannel.createClientUniverseChannel(this, channelNameArg, passphraseArg);
2019-04-22 11:06:01 +00:00
}
/**
* gets a channel from the channelcache
* @param channelName
2019-04-22 22:28:57 +00:00
* @param passphraseArg
2019-04-22 11:06:01 +00:00
*/
2019-04-22 20:04:52 +00:00
public async getChannel(channelName: string): Promise<ClientUniverseChannel> {
2019-04-22 11:06:01 +00:00
await this.checkConnection();
2019-04-22 20:04:52 +00:00
const clientUniverseChannel = this.channelCache.find(channel => {
return channel.name === channelName;
2019-04-22 22:28:57 +00:00
});
2019-04-22 11:06:01 +00:00
return clientUniverseChannel;
}
2019-04-22 07:58:36 +00:00
public async sendMessage(messageArg: interfaces.IMessageCreator) {
2019-04-24 16:20:31 +00:00
await this.checkConnection();
2019-04-22 07:58:36 +00:00
const requestBody: interfaces.IUniverseMessage = {
id: plugins.smartunique.shortId(),
timestamp: Date.now(),
2019-04-22 11:06:01 +00:00
passphrase: (await this.getChannel(messageArg.targetChannelName)).passphrase,
2019-04-22 22:28:57 +00:00
...messageArg
2019-04-22 07:58:36 +00:00
};
2018-04-13 13:45:48 +00:00
// TODO: User websocket connection if available
2019-04-22 22:28:57 +00:00
const response = await plugins.smartrequest.postJson(
`${this.options.serverAddress}/sendmessage`,
{
2019-04-24 16:20:31 +00:00
requestBody
2019-04-22 22:28:57 +00:00
}
);
2018-03-13 05:15:40 +00:00
}
2018-04-13 13:45:48 +00:00
public close() {
2019-04-24 21:27:57 +00:00
this.smartsocketClient.disconnect();
}
2019-04-22 11:06:01 +00:00
/**
* 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> {
2019-04-24 21:27:57 +00:00
if (!this.smartsocketClient && !this.observableIntake) {
2018-03-20 07:16:54 +00:00
const parsedURL = url.parse(this.options.serverAddress);
2019-04-24 16:20:31 +00:00
const socketConfig: plugins.smartsocket.ISmartsocketClientOptions = {
2018-03-20 07:16:54 +00:00
alias: process.env.SOCKET_ALIAS || 'someclient',
password: 'UniverseClient',
port: parseInt(parsedURL.port, 10),
role: 'UniverseClient',
2019-04-24 16:20:31 +00:00
url: parsedURL.protocol + '//' + parsedURL.hostname
};
console.log(socketConfig);
2019-04-24 21:27:57 +00:00
this.smartsocketClient = new SmartsocketClient(socketConfig);
2018-03-20 07:16:54 +00:00
this.observableIntake = new plugins.smartrx.ObservableIntake();
2019-04-24 21:27:57 +00:00
// lets define some basic actions
/**
* should handle a forced unsubscription by the server
*/
const unsubscribe = new plugins.smartsocket.SocketFunction({
funcName: 'unsubscribe',
allowedRoles: [],
funcDef: async () => {},
});
/**
* should handle a message reception
*/
const receiveMessage = async () => {
};
await this.smartsocketClient.connect();
2018-03-20 07:16:54 +00:00
}
}
2018-03-15 00:05:13 +00:00
}