Compare commits

..

4 Commits

Author SHA1 Message Date
424d9cca91 1.1.58 2019-11-08 18:48:39 +01:00
d712270946 fix(core): update 2019-11-08 18:48:39 +01:00
f98c797ad8 1.1.57 2019-11-08 18:41:09 +01:00
a23e7349be fix(core): update 2019-11-08 18:41:08 +01:00
5 changed files with 34 additions and 7 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartsocket", "name": "@pushrocks/smartsocket",
"version": "1.1.56", "version": "1.1.58",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartsocket", "name": "@pushrocks/smartsocket",
"version": "1.1.56", "version": "1.1.58",
"description": "easy and secure websocket communication", "description": "easy and secure websocket communication",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",

View File

@ -2,4 +2,4 @@ export interface IRequestAuthPayload {
serverShortId: string; serverShortId: string;
} }
export type TConnectionEvent = 'terminated' | 'error'; export type TConnectionStatus = 'new' | 'connecting' | 'connected' | 'disconnecting' | 'disconnected';

View File

@ -33,7 +33,9 @@ export class SmartsocketClient {
public serverPort: number; public serverPort: number;
public autoReconnect: boolean; public autoReconnect: boolean;
public eventSubject = new plugins.smartrx.rxjs.Subject<interfaces.TConnectionEvent>(); // status handling
public eventSubject = new plugins.smartrx.rxjs.Subject<interfaces.TConnectionStatus>();
public eventStatus: interfaces.TConnectionStatus = 'new';
public socketFunctions = new plugins.lik.Objectmap<SocketFunction<any>>(); public socketFunctions = new plugins.lik.Objectmap<SocketFunction<any>>();
public socketRequests = new plugins.lik.Objectmap<SocketRequest<any>>(); public socketRequests = new plugins.lik.Objectmap<SocketRequest<any>>();
@ -110,6 +112,11 @@ export class SmartsocketClient {
}); });
// handle connection
this.socketConnection.socket.on('connect', async () => {
this.updateStatus('connected');
});
// handle disconnection and errors // handle disconnection and errors
this.socketConnection.socket.on('disconnect', async () => { this.socketConnection.socket.on('disconnect', async () => {
await this.disconnect(); await this.disconnect();
@ -135,7 +142,7 @@ export class SmartsocketClient {
} }
defaultLogger.log('warn', `disconnected from server ${this.remoteShortId}`); defaultLogger.log('warn', `disconnected from server ${this.remoteShortId}`);
this.remoteShortId = null; this.remoteShortId = null;
this.eventSubject.next('terminated'); this.updateStatus('disconnected');
if (this.autoReconnect) { if (this.autoReconnect) {
this.tryDebouncedReconnect(); this.tryDebouncedReconnect();
@ -170,4 +177,11 @@ export class SmartsocketClient {
const result = response.funcDataArg; const result = response.funcDataArg;
return result; return result;
} }
private updateStatus (statusArg: interfaces.TConnectionStatus) {
if (this.eventStatus !== statusArg) {
this.eventSubject.next(statusArg);
}
this.eventStatus = statusArg;
}
} }

View File

@ -55,7 +55,8 @@ export class SocketConnection {
public smartsocketRef: Smartsocket | SmartsocketClient; public smartsocketRef: Smartsocket | SmartsocketClient;
public socket: SocketIO.Socket | SocketIOClient.Socket; public socket: SocketIO.Socket | SocketIOClient.Socket;
public eventSubject = new plugins.smartrx.rxjs.Subject<interfaces.TConnectionEvent>(); public eventSubject = new plugins.smartrx.rxjs.Subject<interfaces.TConnectionStatus>();
public eventStatus: interfaces.TConnectionStatus = 'new';
constructor(optionsArg: ISocketConnectionConstructorOptions) { constructor(optionsArg: ISocketConnectionConstructorOptions) {
this.alias = optionsArg.alias; this.alias = optionsArg.alias;
@ -67,6 +68,11 @@ export class SocketConnection {
// standard behaviour that is always true // standard behaviour that is always true
allSocketConnections.add(this); allSocketConnections.add(this);
// handle connection
this.socket.on('connect', async () => {
this.updateStatus('connected');
});
this.socket.on('disconnect', async () => { this.socket.on('disconnect', async () => {
plugins.smartlog.defaultLogger.log( plugins.smartlog.defaultLogger.log(
'info', 'info',
@ -173,6 +179,13 @@ export class SocketConnection {
// disconnecting ---------------------- // disconnecting ----------------------
public async disconnect() { public async disconnect() {
this.socket.disconnect(true); this.socket.disconnect(true);
this.eventSubject.next('terminated'); this.updateStatus('disconnected');
}
private updateStatus (statusArg: interfaces.TConnectionStatus) {
if (this.eventStatus !== statusArg) {
this.eventSubject.next(statusArg);
}
this.eventStatus = statusArg;
} }
} }