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