|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { Plugin, Cordova, AwesomeCordovaNativePlugin, CordovaProperty } from '@awesome-cordova-plugins/core'; |
| 3 | +import { Observable, fromEventPattern } from 'rxjs'; |
| 4 | +import { filter, map } from 'rxjs/operators'; |
| 5 | + |
| 6 | +/** |
| 7 | + * @name SocketsTcp |
| 8 | + * @description |
| 9 | + * This plugin provides TCP client sockets for Android and iOS. |
| 10 | + * @usage |
| 11 | + * ```typescript |
| 12 | + * import { SocketsTcp } from '@awesome-cordova-plugins/sockets-tcp/ngx'; |
| 13 | + * |
| 14 | + * constructor(private socketsTcp: SocketsTcp) { } |
| 15 | + * |
| 16 | + * ... |
| 17 | + * |
| 18 | + * this.platform.ready().then(() => { |
| 19 | + * this.socketsTcp.getSockets() |
| 20 | + * .then((result: any) => console.log(res)) |
| 21 | + * .catch((error: any) => console.error(error)); |
| 22 | + * }) |
| 23 | + * |
| 24 | + * ``` |
| 25 | + */ |
| 26 | +@Plugin({ |
| 27 | + pluginName: 'SocketsTcp', |
| 28 | + plugin: 'cordova-plugin-chrome-apps-sockets-tcp', |
| 29 | + pluginRef: 'chrome.sockets.tcp', |
| 30 | + repo: 'https://github.com/KoenLav/cordova-plugin-chrome-apps-sockets-tcp', |
| 31 | + install: 'ionic cordova plugin add https://github.com/KoenLav/cordova-plugin-chrome-apps-sockets-tcp', |
| 32 | + platforms: ['Android', 'iOS'], |
| 33 | +}) |
| 34 | +@Injectable() |
| 35 | +export class SocketsTcp extends AwesomeCordovaNativePlugin { |
| 36 | + @CordovaProperty() |
| 37 | + onReceive: SocketTcpEvent; |
| 38 | + |
| 39 | + @CordovaProperty() |
| 40 | + onReceiveError: SocketTcpEvent; |
| 41 | + |
| 42 | + /** |
| 43 | + * @param properties |
| 44 | + */ |
| 45 | + @Cordova() |
| 46 | + create(properties: any): Promise<SocketTcpInfo> { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * |
| 52 | + * @param socketId |
| 53 | + * @param properties |
| 54 | + */ |
| 55 | + @Cordova() |
| 56 | + update(socketId: number, properties: any): Promise<any> { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * |
| 62 | + * @param socketId |
| 63 | + * @param paused |
| 64 | + */ |
| 65 | + @Cordova() |
| 66 | + setPaused(socketId: number, paused: boolean): Promise<any> { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * |
| 72 | + * @param socketId |
| 73 | + * @param enabled |
| 74 | + * @param delay |
| 75 | + */ |
| 76 | + @Cordova() |
| 77 | + setKeepAlive(socketId: number, enabled: boolean, delay: any): Promise<any> { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * |
| 83 | + * @param socketId |
| 84 | + * @param noDelay |
| 85 | + */ |
| 86 | + @Cordova({ |
| 87 | + platforms: ['Android'], |
| 88 | + }) |
| 89 | + setNoDelay(socketId: number, noDelay: any): Promise<any> { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * |
| 95 | + * @param socketId |
| 96 | + * @param peerAddress |
| 97 | + * @param peerPort |
| 98 | + */ |
| 99 | + @Cordova() |
| 100 | + connect(socketId: number, peerAddress: string, peerPort: number): Promise<any> { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * |
| 106 | + * @param socketId |
| 107 | + */ |
| 108 | + @Cordova() |
| 109 | + disconnect(socketId: number): Promise<any> { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * |
| 115 | + * @param socketId |
| 116 | + * @param options |
| 117 | + */ |
| 118 | + @Cordova() |
| 119 | + secure(socketId: number, options: any): Promise<any> { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * |
| 125 | + * @param socketId |
| 126 | + * @param data |
| 127 | + */ |
| 128 | + @Cordova() |
| 129 | + send(socketId: number, data: ArrayBuffer): Promise<any> { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * |
| 135 | + * @param socketId |
| 136 | + */ |
| 137 | + @Cordova() |
| 138 | + close(socketId: number): Promise<any> { |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * |
| 144 | + * @param socketId |
| 145 | + */ |
| 146 | + @Cordova() |
| 147 | + getInfo(socketId: number): Promise<any> { |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * |
| 153 | + */ |
| 154 | + @Cordova() |
| 155 | + getSockets(): Promise<SocketTcpInfo[]> { |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * |
| 161 | + * @param socketId |
| 162 | + * @param options |
| 163 | + */ |
| 164 | + @Cordova() |
| 165 | + pipeToFile(socketId: number, options: any): Promise<any> { |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Watch all incoming data event |
| 171 | + */ |
| 172 | + public onReceiveData(): Observable<SocketTcpDataInfo> { |
| 173 | + return fromEventPattern( |
| 174 | + (eventHandler) => this.onReceive.addListener(eventHandler), |
| 175 | + (errorEventHandler) => this.onReceive.removeListener(errorEventHandler) |
| 176 | + ).pipe( |
| 177 | + map((socketUdpDataInfo: SocketTcpDataInfo) => { |
| 178 | + socketUdpDataInfo.dataAsSting = socketUdpDataInfo.data |
| 179 | + ? new TextDecoder().decode(socketUdpDataInfo.data).trim() |
| 180 | + : null; |
| 181 | + return socketUdpDataInfo; |
| 182 | + }) |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Watch socket incoming data |
| 188 | + * @param socketId |
| 189 | + */ |
| 190 | + public onReceiveDataBySocketId(socketId: number) { |
| 191 | + return this.onReceiveData().pipe(filter((socketDataInfo) => socketDataInfo.socketId === socketId)); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Watch all sockets incoming error event listener |
| 196 | + */ |
| 197 | + public onReceiveDataError(): Observable<SocketTcpErrorInfo> { |
| 198 | + return fromEventPattern( |
| 199 | + (eventHandler) => this.onReceiveError.addListener(eventHandler), |
| 200 | + (errorEventHandler) => this.onReceiveError.removeListener(errorEventHandler) |
| 201 | + ); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Watch socket incoming error event listener |
| 206 | + * @param socketId |
| 207 | + */ |
| 208 | + public onReceiveDataErrorBySocketId(socketId: number) { |
| 209 | + return this.onReceiveDataError().pipe(filter((socketDataInfo) => socketDataInfo.socketId === socketId)); |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +export interface SocketTcpInfo { |
| 214 | + socketId: number; |
| 215 | + persistent?: boolean; |
| 216 | + bufferSize?: number; |
| 217 | + connected?: boolean; |
| 218 | + name?: string; |
| 219 | + paused?: boolean; |
| 220 | + localAddress?: string; |
| 221 | + localPort?: number; |
| 222 | + peerAddress?: string; |
| 223 | + peerPort?: number; |
| 224 | + [key: string]: any; |
| 225 | +} |
| 226 | + |
| 227 | +export interface SocketTcpDataInfo { |
| 228 | + socketId: number; |
| 229 | + uri: string; |
| 230 | + bytesRead: number; |
| 231 | + data: ArrayBuffer; |
| 232 | + dataAsSting: string; |
| 233 | +} |
| 234 | + |
| 235 | +export interface SocketTcpErrorInfo { |
| 236 | + message: string; |
| 237 | + resultCode: SocketTcpErrorResultCode; |
| 238 | + socketId: number; |
| 239 | + e?: boolean; |
| 240 | +} |
| 241 | + |
| 242 | +export enum SocketTcpErrorResultCode { |
| 243 | + SocketClosedByServer = 1, |
| 244 | + ConnectionTimedOut = 2, |
| 245 | + GenericSocketError = 3, |
| 246 | + SocketNotConnected = 4, |
| 247 | + ConnectionRefused = 5, |
| 248 | +} |
| 249 | + |
| 250 | +interface SocketTcpEvent { |
| 251 | + addListener(cb: (...args: any[]) => void): void; |
| 252 | + |
| 253 | + removeListener(cb: (...args: any[]) => void): void; |
| 254 | + |
| 255 | + fire(): void; |
| 256 | + |
| 257 | + hasListener(): boolean; |
| 258 | + |
| 259 | + hasListeners(): boolean; |
| 260 | + |
| 261 | + // Stub |
| 262 | + addRules(): void; |
| 263 | + |
| 264 | + // Stub |
| 265 | + getRules(): void; |
| 266 | + |
| 267 | + // Stub |
| 268 | + removeRules(): void; |
| 269 | +} |
0 commit comments