ferrum-db-client
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -37,3 +37,3 @@ /// <reference types="node" /> | ||
protected id: number; | ||
readonly socket: Socket; | ||
socket: Socket; | ||
protected pending: Map<number, (buffer: Buffer) => void>; | ||
@@ -45,3 +45,5 @@ private writeBuffer; | ||
private lastResponse; | ||
private heartBeatInterval; | ||
constructor(socket: Socket); | ||
private initialize; | ||
reconnect(): void; | ||
@@ -48,0 +50,0 @@ disconnect(): void; |
@@ -47,26 +47,12 @@ "use strict"; | ||
socket.connect(port, ip); | ||
let connected = false; | ||
let client; | ||
let beatInterval; | ||
socket.once("connect", () => { | ||
connected = true; | ||
client = new FerrumServerClient(socket); | ||
resolve(new FerrumServerConnection(client)); | ||
beatInterval = setInterval(() => { | ||
client.heartbeat(); | ||
}, 2000); | ||
}); | ||
socket.once("error", (e) => { | ||
clearInterval(beatInterval); | ||
console.error(e); | ||
reject(e); | ||
}); | ||
socket.once("end", () => { | ||
clearInterval(beatInterval); | ||
if (connected && !client.disposed) { | ||
client.reconnect(); | ||
} | ||
}); | ||
socket.once("timeout", () => { | ||
clearInterval(beatInterval); | ||
console.error("Connection timeout"); | ||
@@ -83,2 +69,8 @@ reject(new Error("Connection timeout")); | ||
this.lastResponse = Date.now(); | ||
this.initialize(socket); | ||
this.heartBeatInterval = setInterval(() => { | ||
this.heartbeat(); | ||
}, 2000); | ||
} | ||
initialize(socket) { | ||
this.socket = socket; | ||
@@ -96,2 +88,7 @@ this.writeBuffer = Buffer.alloc(8192); | ||
let readOffset = 0; | ||
socket.on("close", () => { | ||
if (!this.disposed) { | ||
this.reconnect(); | ||
} | ||
}); | ||
this.socket.on("data", (data) => { | ||
@@ -137,2 +134,8 @@ this.lastResponse = Date.now(); | ||
this.disposed = false; | ||
this.heartBeatPending = false; | ||
if (!this.heartBeatInterval) { | ||
this.heartBeatInterval = setInterval(() => { | ||
this.heartbeat(); | ||
}, 2000); | ||
} | ||
this.socket.connect(this.socket.remotePort, this.socket.remoteAddress); | ||
@@ -148,2 +151,4 @@ for (const pendingId of this.pending.keys()) { | ||
disconnect() { | ||
clearInterval(this.heartBeatInterval); | ||
this.heartBeatInterval = undefined; | ||
this.disposed = true; | ||
@@ -164,2 +169,3 @@ this.socket.destroy(); | ||
const response = await this.getResponse(myId); | ||
this.heartBeatPending = false; | ||
const br = getBinaryReader(response); | ||
@@ -166,0 +172,0 @@ const success = br.readBoolean(); |
{ | ||
"name": "ferrum-db-client", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"main": "dist/client.js", | ||
@@ -5,0 +5,0 @@ "typings": "dist/client.d.ts", |
@@ -58,16 +58,9 @@ import { Socket } from "net"; | ||
socket.connect(port, ip); | ||
let connected: boolean = false; | ||
let client: FerrumServerClient; | ||
let beatInterval: NodeJS.Timeout; | ||
socket.once("connect", () => { | ||
connected = true; | ||
client = new FerrumServerClient(socket); | ||
resolve(new FerrumServerConnection(client)); | ||
beatInterval = setInterval(() => { | ||
client.heartbeat(); | ||
}, 2000); | ||
}); | ||
socket.once("error", (e) => { | ||
clearInterval(beatInterval); | ||
console.error(e); | ||
@@ -77,10 +70,3 @@ reject(e); | ||
socket.once("end", () => { | ||
clearInterval(beatInterval); | ||
if (connected && !client.disposed) { | ||
client.reconnect(); | ||
} | ||
}); | ||
socket.once("timeout", () => { | ||
clearInterval(beatInterval); | ||
console.error("Connection timeout"); | ||
@@ -94,3 +80,3 @@ reject(new Error("Connection timeout")); | ||
protected id: number = 0; | ||
public readonly socket: Socket; | ||
public socket: Socket; | ||
protected pending: Map<number, (buffer: Buffer) => void>; | ||
@@ -102,4 +88,12 @@ private writeBuffer: Buffer; | ||
private lastResponse: number = Date.now(); | ||
private heartBeatInterval: NodeJS.Timeout; | ||
constructor(socket: Socket) { | ||
this.initialize(socket); | ||
this.heartBeatInterval = setInterval(() => { | ||
this.heartbeat(); | ||
}, 2000); | ||
} | ||
private initialize(socket: Socket) { | ||
this.socket = socket; | ||
@@ -117,2 +111,9 @@ this.writeBuffer = Buffer.alloc(8192); | ||
let readOffset = 0; | ||
socket.on("close", () => { | ||
if (!this.disposed) { | ||
this.reconnect(); | ||
} | ||
}); | ||
this.socket.on("data", (data: Buffer) => { | ||
@@ -172,2 +173,8 @@ this.lastResponse = Date.now(); | ||
this.disposed = false; | ||
this.heartBeatPending = false; | ||
if (!this.heartBeatInterval) { | ||
this.heartBeatInterval = setInterval(() => { | ||
this.heartbeat(); | ||
}, 2000); | ||
} | ||
this.socket.connect(this.socket.remotePort, this.socket.remoteAddress); | ||
@@ -184,2 +191,4 @@ for (const pendingId of this.pending.keys()) { | ||
public disconnect(): void { | ||
clearInterval(this.heartBeatInterval); | ||
this.heartBeatInterval = undefined; | ||
this.disposed = true; | ||
@@ -204,2 +213,3 @@ this.socket.destroy(); | ||
const response = await this.getResponse(myId); | ||
this.heartBeatPending = false; | ||
const br = getBinaryReader(response); | ||
@@ -206,0 +216,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
118141
1596