Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Merge 47335be into 9771b1f
Browse files Browse the repository at this point in the history
  • Loading branch information
jperata committed Dec 4, 2017
2 parents 9771b1f + 47335be commit b72f42d
Show file tree
Hide file tree
Showing 2 changed files with 220 additions and 172 deletions.
39 changes: 30 additions & 9 deletions lib/client/bespoke-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ export class BespokeClient {
public onConnect: (error?: any) => void = null;
public onError: (errorType: NetworkErrorType, message: string) => void;

private static RECONNECT_MAX_RETRIES: number = 3;
private keepAlive: KeepAlive;
private socketHandler: SocketHandler;
private shuttingDown: boolean = false;
private reconnectRetries: number = 0;

constructor(public nodeID: string,
private host: string,
Expand All @@ -30,13 +32,9 @@ export class BespokeClient {
private targetPort: number,
private secretKey?: string) {}

public connect(onConnect?: (error?: any) => void): void {
private attemptConnection() {
const self = this;

if (onConnect !== undefined && onConnect !== null) {
this.onConnect = onConnect;
}

this.socketHandler = SocketHandler.connect(this.host, this.port,
function(error: any) {
self.connected(error);
Expand All @@ -46,12 +44,28 @@ export class BespokeClient {
self.messageReceived(data, messageID);
}
);
}
public connect(onConnect?: (error?: any) => void): void {
const self = this;

if (onConnect !== undefined && onConnect !== null) {
this.onConnect = onConnect;
}

this.attemptConnection();

// If the socket gets closed, probably server-side issue
// We do not do anything in this case other than
this.socketHandler.onCloseCallback = function() {
if (!self.shuttingDown) {
LoggingHelper.error(Logger, "Socket closed by bst server: " + self.host + ":" + self.port);
}

if (self.reconnectRetries < BespokeClient.RECONNECT_MAX_RETRIES) {
self.reconnectRetries++;
LoggingHelper.error(Logger, "Attempting to reconnect in " + self.reconnectRetries + " seconds");
self.attemptConnection();
} else {
LoggingHelper.error(Logger, "Check your network settings - and try connecting again.");
LoggingHelper.error(Logger, "If the issue persists, contact us at Bespoken:");
LoggingHelper.error(Logger, "\thttps://gitter.im/bespoken/bst");
Expand Down Expand Up @@ -140,13 +154,20 @@ export class BespokeClient {
}

private connected(error?: any): void {
if (error !== undefined && error !== null) {
if (error) {
LoggingHelper.error(Logger, "Unable to connect to: " + this.host + ":" + this.port);
this.shutdown();
if (this.onConnect !== undefined && this.onConnect !== null) {
this.onConnect(error);
if (this.reconnectRetries < BespokeClient.RECONNECT_MAX_RETRIES) {
this.reconnectRetries++;
LoggingHelper.error(Logger, "Attempting to reconnect in " + this.reconnectRetries + " seconds");
this.attemptConnection();
} else {
this.shutdown();
if (this.onConnect) {
this.onConnect(error);
}
}
} else {
this.reconnectRetries = 0;
LoggingHelper.info(Logger, "Connected - " + this.host + ":" + this.port);
// As soon as we connect, we send our ID
const messageJSON = {"id": this.nodeID};
Expand Down

0 comments on commit b72f42d

Please sign in to comment.