Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add socket error event #66

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/pos.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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", () => {
Expand All @@ -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);
})
Expand Down
10 changes: 9 additions & 1 deletion types/pos.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,7 +21,7 @@ export class TransbankPOSWebSocket extends EventEmitter {

socket(): Socket | null;

connect(socketIoUrl?: string): Promise<boolean>;
connect(socketIoUrl?: string, options?: AgentConnectionOptions): Promise<boolean>;

disconnect(): Promise<boolean>;

Expand Down