parent
be5d42baba
commit
2f6c018b7a
4 changed files with 19 additions and 32 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { ServerStatus, StatusEnum, WebSocketConnectOptions } from 'types';
|
import { StatusEnum, WebSocketConnectOptions } from 'types';
|
||||||
|
|
||||||
import { ProtobufService } from './services/ProtobufService';
|
import { ProtobufService } from './services/ProtobufService';
|
||||||
import { WebSocketService } from './services/WebSocketService';
|
import { WebSocketService } from './services/WebSocketService';
|
||||||
|
@ -37,6 +37,7 @@ export class WebClient {
|
||||||
};
|
};
|
||||||
|
|
||||||
public options: WebSocketConnectOptions;
|
public options: WebSocketConnectOptions;
|
||||||
|
public status: StatusEnum;
|
||||||
|
|
||||||
public connectionAttemptMade = false;
|
public connectionAttemptMade = false;
|
||||||
|
|
||||||
|
@ -45,10 +46,6 @@ export class WebClient {
|
||||||
this.protobuf.handleMessageEvent(message);
|
this.protobuf.handleMessageEvent(message);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.socket.statusChange$.subscribe((status: ServerStatus) => {
|
|
||||||
this.handleStatusChange(status);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (process.env.NODE_ENV !== 'test') {
|
if (process.env.NODE_ENV !== 'test') {
|
||||||
console.log(this);
|
console.log(this);
|
||||||
}
|
}
|
||||||
|
@ -68,12 +65,8 @@ export class WebClient {
|
||||||
this.socket.disconnect();
|
this.socket.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
public updateStatus(status: StatusEnum, description: string) {
|
public updateStatus(status: StatusEnum) {
|
||||||
this.socket.updateStatus(status, description);
|
this.status = status;
|
||||||
}
|
|
||||||
|
|
||||||
public handleStatusChange({ status, description }: ServerStatus) {
|
|
||||||
SessionPersistence.updateStatus(status, description);
|
|
||||||
|
|
||||||
if (status === StatusEnum.DISCONNECTED) {
|
if (status === StatusEnum.DISCONNECTED) {
|
||||||
this.protobuf.resetCommands();
|
this.protobuf.resetCommands();
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
import { StatusEnum } from 'types'
|
import { StatusEnum } from 'types';
|
||||||
import webClient from '../../WebClient'
|
import webClient from '../../WebClient';
|
||||||
|
import { SessionPersistence } from '../../persistence';
|
||||||
|
|
||||||
export function updateStatus(status: StatusEnum, description: string): void {
|
export function updateStatus(status: StatusEnum, description: string): void {
|
||||||
webClient.updateStatus(status, description);
|
SessionPersistence.updateStatus(status, description);
|
||||||
|
|
||||||
|
webClient.updateStatus(status);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,14 @@
|
||||||
import { KeepAliveService } from './KeepAliveService';
|
import { KeepAliveService } from './KeepAliveService';
|
||||||
import { WebSocketService } from './WebSocketService';
|
|
||||||
|
|
||||||
import webClient from '../WebClient';
|
import webClient from '../WebClient';
|
||||||
|
|
||||||
describe('KeepAliveService', () => {
|
describe('KeepAliveService', () => {
|
||||||
let service: KeepAliveService;
|
let service: KeepAliveService;
|
||||||
let socket: WebSocketService;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.useFakeTimers();
|
jest.useFakeTimers();
|
||||||
|
|
||||||
socket = new WebSocketService(webClient);
|
service = new KeepAliveService(webClient.socket);
|
||||||
service = new KeepAliveService(socket);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create', () => {
|
it('should create', () => {
|
||||||
|
@ -30,7 +27,7 @@ describe('KeepAliveService', () => {
|
||||||
promise = new Promise(resolve => resolvePing = resolve);
|
promise = new Promise(resolve => resolvePing = resolve);
|
||||||
ping = (done) => promise.then(done);
|
ping = (done) => promise.then(done);
|
||||||
|
|
||||||
checkReadyStateSpy = jest.spyOn(socket, 'checkReadyState');
|
checkReadyStateSpy = jest.spyOn(webClient.socket, 'checkReadyState');
|
||||||
checkReadyStateSpy.mockImplementation(() => true);
|
checkReadyStateSpy.mockImplementation(() => true);
|
||||||
|
|
||||||
service.startPingLoop(interval, ping);
|
service.startPingLoop(interval, ping);
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import { Subject } from 'rxjs';
|
import { Subject } from 'rxjs';
|
||||||
|
|
||||||
import { ServerStatus, StatusEnum, WebSocketConnectOptions } from 'types';
|
import { StatusEnum, WebSocketConnectOptions } from 'types';
|
||||||
|
|
||||||
import { KeepAliveService } from './KeepAliveService';
|
import { KeepAliveService } from './KeepAliveService';
|
||||||
import { WebClient } from '../WebClient';
|
import { WebClient } from '../WebClient';
|
||||||
import { SessionPersistence } from '../persistence';
|
import { SessionPersistence } from '../persistence';
|
||||||
|
import { updateStatus } from '../commands/session';
|
||||||
|
|
||||||
export class WebSocketService {
|
export class WebSocketService {
|
||||||
private socket: WebSocket;
|
private socket: WebSocket;
|
||||||
|
@ -14,9 +15,7 @@ export class WebSocketService {
|
||||||
private keepAliveService: KeepAliveService;
|
private keepAliveService: KeepAliveService;
|
||||||
|
|
||||||
public message$: Subject<MessageEvent> = new Subject();
|
public message$: Subject<MessageEvent> = new Subject();
|
||||||
public statusChange$: Subject<ServerStatus> = new Subject();
|
|
||||||
|
|
||||||
private status: StatusEnum = StatusEnum.DISCONNECTED;
|
|
||||||
private keepalive: number;
|
private keepalive: number;
|
||||||
|
|
||||||
constructor(webClient: WebClient) {
|
constructor(webClient: WebClient) {
|
||||||
|
@ -25,7 +24,7 @@ export class WebSocketService {
|
||||||
this.keepAliveService = new KeepAliveService(this);
|
this.keepAliveService = new KeepAliveService(this);
|
||||||
this.keepAliveService.disconnected$.subscribe(() => {
|
this.keepAliveService.disconnected$.subscribe(() => {
|
||||||
this.disconnect();
|
this.disconnect();
|
||||||
this.updateStatus(StatusEnum.DISCONNECTED, 'Connection timeout');
|
updateStatus(StatusEnum.DISCONNECTED, 'Connection timeout');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,11 +63,6 @@ export class WebSocketService {
|
||||||
this.socket.send(message);
|
this.socket.send(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public updateStatus(status: StatusEnum, description: string): void {
|
|
||||||
this.status = status;
|
|
||||||
this.statusChange$.next({ status, description });
|
|
||||||
}
|
|
||||||
|
|
||||||
private createWebSocket(url: string): WebSocket {
|
private createWebSocket(url: string): WebSocket {
|
||||||
const socket = new WebSocket(url);
|
const socket = new WebSocket(url);
|
||||||
socket.binaryType = 'arraybuffer';
|
socket.binaryType = 'arraybuffer';
|
||||||
|
@ -77,7 +71,7 @@ export class WebSocketService {
|
||||||
|
|
||||||
socket.onopen = () => {
|
socket.onopen = () => {
|
||||||
clearTimeout(connectionTimer);
|
clearTimeout(connectionTimer);
|
||||||
this.updateStatus(StatusEnum.CONNECTED, 'Connected');
|
updateStatus(StatusEnum.CONNECTED, 'Connected');
|
||||||
|
|
||||||
this.keepAliveService.startPingLoop(this.keepalive, (pingReceived: Function) => {
|
this.keepAliveService.startPingLoop(this.keepalive, (pingReceived: Function) => {
|
||||||
this.webClient.keepAlive(pingReceived);
|
this.webClient.keepAlive(pingReceived);
|
||||||
|
@ -86,15 +80,15 @@ export class WebSocketService {
|
||||||
|
|
||||||
socket.onclose = () => {
|
socket.onclose = () => {
|
||||||
// dont overwrite failure messages
|
// dont overwrite failure messages
|
||||||
if (this.status !== StatusEnum.DISCONNECTED) {
|
if (this.webClient.status !== StatusEnum.DISCONNECTED) {
|
||||||
this.updateStatus(StatusEnum.DISCONNECTED, 'Connection Closed');
|
updateStatus(StatusEnum.DISCONNECTED, 'Connection Closed');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.keepAliveService.endPingLoop();
|
this.keepAliveService.endPingLoop();
|
||||||
};
|
};
|
||||||
|
|
||||||
socket.onerror = () => {
|
socket.onerror = () => {
|
||||||
this.updateStatus(StatusEnum.DISCONNECTED, 'Connection Failed');
|
updateStatus(StatusEnum.DISCONNECTED, 'Connection Failed');
|
||||||
SessionPersistence.connectionFailed();
|
SessionPersistence.connectionFailed();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue