diff --git a/src/pos.js b/src/pos.js index 7397b54..31e33cc 100644 --- a/src/pos.js +++ b/src/pos.js @@ -6,6 +6,15 @@ import "regenerator-runtime/runtime"; import * as io from "socket.io-client" export class TransbankPOSWebSocket extends EventEmitter { + + defaultConnectionOptions = { + reconnection: true, + reconnectionAttempts: 10, + reconnectionDelay: 1000, + reconnectionDelayMax: 5000, + autoConnect: true, + } + constructor() { super() this.isConnected = false @@ -23,8 +32,8 @@ export class TransbankPOSWebSocket extends EventEmitter { return this.socket; } - async connect(socketIoUrl = "https://localhost:8090") { - this.socket = io(socketIoUrl) + async connect(socketIoUrl = "https://localhost:8090", options = this.defaultConnectionOptions) { + this.socket = io(socketIoUrl, options) this.isConnected = true this.socket.on("connect", () => { @@ -37,6 +46,14 @@ export class TransbankPOSWebSocket extends EventEmitter { this.emit('socket_disconnected'); }); + this.socket.on("connect_error", (error) => { + this.emit('socket_connection_error', error); + }); + + this.socket.on("reconnect_failed", (error) => { + this.emit('socket_connection_failed', error); + }); + this.socket.on('event.port_opened', (port) => { this.emit('port_opened', port); }) diff --git a/types/pos.d.ts b/types/pos.d.ts index ae51b8d..23894d0 100644 --- a/types/pos.d.ts +++ b/types/pos.d.ts @@ -2,6 +2,14 @@ import { EventEmitter } from 'events'; import type { Socket } from 'socket.io-client'; import { SaleResponse, LoadKeysResponse, TotalsResponse, RefundResponse, DetailsResponse, CloseResponse, PortStatusResponse, IntermediateMessageResponse } from './responses'; +export type AgentConnectionOptions = { + reconnection?: boolean; + reconnectionAttempts?: number; + reconnectionDelay?: number; + reconnectionDelayMax?: number; + autoConnect?: boolean; +} + export class TransbankPOSWebSocket extends EventEmitter { isConnected: boolean; debugEnabled: boolean; @@ -13,7 +21,7 @@ export class TransbankPOSWebSocket extends EventEmitter { socket(): Socket | null; - connect(socketIoUrl?: string): Promise; + connect(socketIoUrl?: string, options?: AgentConnectionOptions): Promise; disconnect(): Promise;