Skip to content

Commit

Permalink
Merge pull request #2 from FastyBird/feature/global-instance
Browse files Browse the repository at this point in the history
Use wamp client in global instance
  • Loading branch information
akadlec authored Apr 20, 2023
2 parents 802dc8f + e3a75ac commit dde8525
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fastybird/vue-wamp-v1",
"version": "1.1.0",
"version": "1.2.0",
"description": "WAMPv1 websockets client for VUE 3",
"keywords": [
"ws",
Expand Down
72 changes: 48 additions & 24 deletions src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { Logger } from '@/Logger';
import { RpcCallError } from '@/RpcCallError';
import { MessageCode } from '@/types';
import {
IWampClient,
IWampLogger,
IWampRpCall,
IWampSubscription,
MessageCode,
OnCloseCallback,
OnConnectCallback,
OnDisconnectCallback,
OnOpenCallback,
RpCallResponse,
SubscribeCallback,
IWampClient,
IWampLogger,
IWampRpCall,
IWampSubscription,
} from '@/types';
import { Ref, ref } from 'vue';

export class Client implements IWampClient {
private readonly wsuri: string;
private wsuriValue: string;

private socket: WebSocket | null;
private sessionId: string | null = null;
Expand All @@ -33,10 +33,10 @@ export class Client implements IWampClient {
public isConnecting: Ref<boolean>;
public isLost: Ref<boolean>;

private logger: IWampLogger;
private clientLogger: IWampLogger;

constructor(host: string, logger: IWampLogger | null) {
this.wsuri = host;
this.wsuriValue = host;

this.socket = null;
this.sessionId = null;
Expand All @@ -53,7 +53,23 @@ export class Client implements IWampClient {
this.isConnected = ref<boolean>(false);
this.isConnecting = ref<boolean>(false);

this.logger = logger === null ? new Logger(false) : logger;
this.clientLogger = logger === null ? new Logger(false) : logger;
}

set host(host: string) {
if (this.isConnected) {
throw new Error('WS host for connected client could not be changed');
}

this.wsuriValue = host;
}

get host(): string {
return this.wsuriValue;
}

set logger(logger: IWampLogger) {
this.logger = logger;
}

public open(): void {
Expand All @@ -63,7 +79,7 @@ export class Client implements IWampClient {

try {
// Open connection to WS server
this.socket = new WebSocket(this.wsuri);
this.socket = new WebSocket(this.wsuriValue);
} catch (e) {
throw new Error('Connection could not be established');
}
Expand Down Expand Up @@ -138,9 +154,9 @@ export class Client implements IWampClient {
this.sessionId = message[0];

if (this.isLost.value) {
this.logger.event('opened re-established connection after lost', this.sessionId, version, server);
this.clientLogger.event('opened re-established connection after lost', this.sessionId, version, server);
} else {
this.logger.event('opened', this.sessionId, version, server);
this.clientLogger.event('opened', this.sessionId, version, server);
}

this.isLost.value = false;
Expand All @@ -156,9 +172,9 @@ export class Client implements IWampClient {
this.subscriptions[index].subscribed = this.send([MessageCode.MSG_SUBSCRIBE, subscription.topic]);

if (!this.subscriptions[index].subscribed) {
this.logger.warn('subscribe failed', subscription.topic);
this.clientLogger.warn('subscribe failed', subscription.topic);
} else {
this.logger.info('subscribed', subscription.topic);
this.clientLogger.info('subscribed', subscription.topic);
}
}
});
Expand Down Expand Up @@ -208,7 +224,7 @@ export class Client implements IWampClient {
}

public subscribe(topic: string, handler: SubscribeCallback): boolean {
this.logger.event('subscribe', topic);
this.clientLogger.event('subscribe', topic);

if (!this.isSubscribed(topic)) {
this.subscriptions.push({
Expand All @@ -227,9 +243,9 @@ export class Client implements IWampClient {
this.subscriptions[index].subscribed = this.send([MessageCode.MSG_SUBSCRIBE, topic]);

if (!this.subscriptions[index].subscribed) {
this.logger.warn('subscribe failed', topic);
this.clientLogger.warn('subscribe failed', topic);
} else {
this.logger.info('subscribed', topic);
this.clientLogger.info('subscribed', topic);
}

return this.subscriptions[index].subscribed;
Expand All @@ -240,7 +256,7 @@ export class Client implements IWampClient {
}

public unsubscribe(topic: string, handler: SubscribeCallback): boolean {
this.logger.event('unsubscribe', topic);
this.clientLogger.event('unsubscribe', topic);

for (let i = 0, len = this.subscriptions.length; i < len; i++) {
if (this.subscriptions[i].topic === topic) {
Expand Down Expand Up @@ -268,14 +284,14 @@ export class Client implements IWampClient {
}

public publish(topic: string, event: string, exclude?: string[] | null, eligible?: string[] | null): boolean {
this.logger.event('publish', topic, event, exclude, eligible);
this.clientLogger.event('publish', topic, event, exclude, eligible);

return this.send([MessageCode.MSG_PUBLISH, event, exclude, eligible]);
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
public call<T>(topic: string, ...data: any): Promise<RpCallResponse<T>> {
this.logger.event('call', topic);
this.clientLogger.event('call', topic);

const callId = Math.random().toString(36).substring(2);

Expand Down Expand Up @@ -361,15 +377,15 @@ export class Client implements IWampClient {
*/
private send(message: any[]): boolean {
if (this.socket === null) {
this.logger.error('not.connected');
this.clientLogger.error('not.connected');

return false;
} else if (this.isConnecting.value) {
this.logger.error('connecting');
this.clientLogger.error('connecting');

return false;
} else if (!this.isConnected.value) {
this.logger.error('lost');
this.clientLogger.error('lost');

return false;
} else {
Expand All @@ -378,7 +394,7 @@ export class Client implements IWampClient {

return true;
} catch (e) {
this.logger.error('send.error');
this.clientLogger.error('send.error');

return false;
}
Expand Down Expand Up @@ -429,3 +445,11 @@ export class Client implements IWampClient {
}
}
}

export function createInstance(host: string, debug = false): IWampClient {
return new Client(host, new Logger(debug));
}

const wampClient = createInstance('ws://localhost:8080');

export default wampClient;
7 changes: 4 additions & 3 deletions src/entry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { App } from 'vue';

// Import library
import { Client } from '@/Client';
import wampClient, { Client } from '@/Client';
import { Logger } from '@/Logger';
import { key, useWampV1Client } from '@/useWampV1Client';
import { InstallFunction, PluginOptions } from '@/types';
Expand All @@ -22,7 +22,8 @@ export function createWampV1Client(): InstallFunction {

const pluginOptions = { ...WampClientDefaultOptions, ...options };

this.wampClient = new Client(pluginOptions.wsuri as string, new Logger(pluginOptions.debug));
wampClient.host = pluginOptions.host;
wampClient.logger = new Logger(pluginOptions.debug);

app.provide(key, this.wampClient);
},
Expand All @@ -31,6 +32,6 @@ export function createWampV1Client(): InstallFunction {
return plugin;
}

export { Client, useWampV1Client };
export { Client, useWampV1Client, wampClient };

export * from '@/types';
8 changes: 7 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export interface IWampClient {
isConnecting: Ref<boolean>;
isLost: Ref<boolean>;

set host(host: string);

get host(): string;

set logger(logger: IWampLogger);

open(): void;

reconnect(): void;
Expand Down Expand Up @@ -78,7 +84,7 @@ export interface IWampLogger {
}

export interface PluginOptions {
wsuri: string;
host: string;
debug: boolean;
autoReestablish: boolean;
autoCloseTimeout: number;
Expand Down

0 comments on commit dde8525

Please sign in to comment.