Skip to content

Commit

Permalink
⌚ Handle socket timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
bdsoha committed Nov 6, 2024
1 parent 110f20a commit 6fb903c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vitrea-client",
"version": "0.0.5",
"version": "0.0.6",
"description": "Vitrea Smart Home API Client",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/VitreaClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('VitreaClient', () => {
expect(mock).toHaveBeenCalledTimes(1)
})

it('[handleDisconnect] will not attempt to reconnect by default', async () => {
it('[handleDisconnect] will not attempt to reconnect', async () => {
const socket = new Socket()

const mock = jest.spyOn(socket, 'connect')
Expand Down
6 changes: 4 additions & 2 deletions src/socket/AbstractSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ export abstract class AbstractSocket extends EventEmitter implements WritableSoc
this.socket = this.socketConfigs.socketSupplier()

return this.socket
.setTimeout(this.socketConfigs.requestTimeout * 10)
.on('connect', this.handleConnect.bind(this))
.on('data', this.handleData.bind(this))
.on('end', this.handleDisconnect.bind(this))
.on('error', this.handleError.bind(this))
}

public async connect() :Promise<void> {
public async connect(): Promise<void> {
this.log.debug('Attempting to make a connection')

if (this.socket) {
Expand Down Expand Up @@ -68,7 +69,7 @@ export abstract class AbstractSocket extends EventEmitter implements WritableSoc
}

public async write(data: Buffer): Promise<void> {
if (!this.socket) {
if (!this.socket || this.socket.destroyed) {
throw new Exceptions.NoConnectionException()
}

Expand Down Expand Up @@ -103,6 +104,7 @@ export abstract class AbstractSocket extends EventEmitter implements WritableSoc

protected handleError(error: Error) {
this.socketConfigs.shouldReconnect = false

this.log.error(`An error occurred - ${error.message}`)
}

Expand Down

0 comments on commit 6fb903c

Please sign in to comment.