From 7782186fb7959d041290058e48c1bcbb7f1d6433 Mon Sep 17 00:00:00 2001 From: Maxim Belov Date: Tue, 17 Sep 2024 13:05:36 +0300 Subject: [PATCH] feat(sockets-tcp): add plugin --- .../plugins/sockets-tcp/index.ts | 269 ++++++++++++++++++ 1 file changed, 269 insertions(+) create mode 100644 src/@awesome-cordova-plugins/plugins/sockets-tcp/index.ts diff --git a/src/@awesome-cordova-plugins/plugins/sockets-tcp/index.ts b/src/@awesome-cordova-plugins/plugins/sockets-tcp/index.ts new file mode 100644 index 0000000000..93f7e64615 --- /dev/null +++ b/src/@awesome-cordova-plugins/plugins/sockets-tcp/index.ts @@ -0,0 +1,269 @@ +import { Injectable } from '@angular/core'; +import { Plugin, Cordova, AwesomeCordovaNativePlugin, CordovaProperty } from '@awesome-cordova-plugins/core'; +import { Observable, fromEventPattern } from 'rxjs'; +import { filter, map } from 'rxjs/operators'; + +/** + * @name SocketsTcp + * @description + * This plugin provides TCP client sockets for Android and iOS. + * @usage + * ```typescript + * import { SocketsTcp } from '@awesome-cordova-plugins/sockets-tcp/ngx'; + * + * constructor(private socketsTcp: SocketsTcp) { } + * + * ... + * + * this.platform.ready().then(() => { + * this.socketsTcp.getSockets() + * .then((result: any) => console.log(res)) + * .catch((error: any) => console.error(error)); + * }) + * + * ``` + */ +@Plugin({ + pluginName: 'SocketsTcp', + plugin: 'cordova-plugin-chrome-apps-sockets-tcp', + pluginRef: 'chrome.sockets.tcp', + repo: 'https://github.com/KoenLav/cordova-plugin-chrome-apps-sockets-tcp', + install: 'ionic cordova plugin add https://github.com/KoenLav/cordova-plugin-chrome-apps-sockets-tcp', + platforms: ['Android', 'iOS'], +}) +@Injectable() +export class SocketsTcp extends AwesomeCordovaNativePlugin { + @CordovaProperty() + onReceive: SocketTcpEvent; + + @CordovaProperty() + onReceiveError: SocketTcpEvent; + + /** + * @param properties + */ + @Cordova() + create(properties: any): Promise { + return; + } + + /** + * + * @param socketId + * @param properties + */ + @Cordova() + update(socketId: number, properties: any): Promise { + return; + } + + /** + * + * @param socketId + * @param paused + */ + @Cordova() + setPaused(socketId: number, paused: boolean): Promise { + return; + } + + /** + * + * @param socketId + * @param enabled + * @param delay + */ + @Cordova() + setKeepAlive(socketId: number, enabled: boolean, delay: any): Promise { + return; + } + + /** + * + * @param socketId + * @param noDelay + */ + @Cordova({ + platforms: ['Android'], + }) + setNoDelay(socketId: number, noDelay: any): Promise { + return; + } + + /** + * + * @param socketId + * @param peerAddress + * @param peerPort + */ + @Cordova() + connect(socketId: number, peerAddress: string, peerPort: number): Promise { + return; + } + + /** + * + * @param socketId + */ + @Cordova() + disconnect(socketId: number): Promise { + return; + } + + /** + * + * @param socketId + * @param options + */ + @Cordova() + secure(socketId: number, options: any): Promise { + return; + } + + /** + * + * @param socketId + * @param data + */ + @Cordova() + send(socketId: number, data: ArrayBuffer): Promise { + return; + } + + /** + * + * @param socketId + */ + @Cordova() + close(socketId: number): Promise { + return; + } + + /** + * + * @param socketId + */ + @Cordova() + getInfo(socketId: number): Promise { + return; + } + + /** + * + */ + @Cordova() + getSockets(): Promise { + return; + } + + /** + * + * @param socketId + * @param options + */ + @Cordova() + pipeToFile(socketId: number, options: any): Promise { + return; + } + + /** + * Watch all incoming data event + */ + public onReceiveData(): Observable { + return fromEventPattern( + (eventHandler) => this.onReceive.addListener(eventHandler), + (errorEventHandler) => this.onReceive.removeListener(errorEventHandler) + ).pipe( + map((socketUdpDataInfo: SocketTcpDataInfo) => { + socketUdpDataInfo.dataAsSting = socketUdpDataInfo.data + ? new TextDecoder().decode(socketUdpDataInfo.data).trim() + : null; + return socketUdpDataInfo; + }) + ); + } + + /** + * Watch socket incoming data + * @param socketId + */ + public onReceiveDataBySocketId(socketId: number) { + return this.onReceiveData().pipe(filter((socketDataInfo) => socketDataInfo.socketId === socketId)); + } + + /** + * Watch all sockets incoming error event listener + */ + public onReceiveDataError(): Observable { + return fromEventPattern( + (eventHandler) => this.onReceiveError.addListener(eventHandler), + (errorEventHandler) => this.onReceiveError.removeListener(errorEventHandler) + ); + } + + /** + * Watch socket incoming error event listener + * @param socketId + */ + public onReceiveDataErrorBySocketId(socketId: number) { + return this.onReceiveDataError().pipe(filter((socketDataInfo) => socketDataInfo.socketId === socketId)); + } +} + +export interface SocketTcpInfo { + socketId: number; + persistent?: boolean; + bufferSize?: number; + connected?: boolean; + name?: string; + paused?: boolean; + localAddress?: string; + localPort?: number; + peerAddress?: string; + peerPort?: number; + [key: string]: any; +} + +export interface SocketTcpDataInfo { + socketId: number; + uri: string; + bytesRead: number; + data: ArrayBuffer; + dataAsSting: string; +} + +export interface SocketTcpErrorInfo { + message: string; + resultCode: SocketTcpErrorResultCode; + socketId: number; + e?: boolean; +} + +export enum SocketTcpErrorResultCode { + SocketClosedByServer = 1, + ConnectionTimedOut = 2, + GenericSocketError = 3, + SocketNotConnected = 4, + ConnectionRefused = 5, +} + +interface SocketTcpEvent { + addListener(cb: (...args: any[]) => void): void; + + removeListener(cb: (...args: any[]) => void): void; + + fire(): void; + + hasListener(): boolean; + + hasListeners(): boolean; + + // Stub + addRules(): void; + + // Stub + getRules(): void; + + // Stub + removeRules(): void; +}