diff --git a/src/@ionic-native/plugins/web-socket-server/index.ts b/src/@ionic-native/plugins/web-socket-server/index.ts new file mode 100644 index 000000000..139890d02 --- /dev/null +++ b/src/@ionic-native/plugins/web-socket-server/index.ts @@ -0,0 +1,210 @@ +import { Injectable } from '@angular/core'; +import { Plugin, Cordova, IonicNativePlugin } from '@ionic-native/core'; +import { Observable } from 'rxjs'; + +declare const window: any; + +export interface WebSocketInterfaces { + [key: string]: WebSocketInterface; +} + +export interface WebSocketInterface { + ipv4Addresses: string[]; + ipv6Addresses: string[]; +} + +export interface WebSocketOptions { + origins?: string[]; + protocols?: string[]; + tcpNoDelay?: boolean; +} + +export interface WebSocketIdentifier { + uuid: string; +} + +export interface WebSocketServerDetails { + addr: string; + port: number; +} + +export interface WebSocketFailure extends WebSocketServerDetails { + reason: string; +} + +export interface WebSocketMessage { + conn: WebSocketConnection; + msg: string; +} + +export interface WebSocketClose { + conn: WebSocketConnection; + code: number; + reason: string; + wasClean: boolean; +} + +export interface WebSocketConnection extends WebSocketIdentifier { + remoteAttr: string; + state: 'open' | 'closed'; + httpFields: HttpFields; + resource: string; +} + +export interface HttpFields { + 'Accept-Encoding': string; + 'Accept-Language': string; + 'Cache-Control': string; + Connection: string; + Host: string; + Origin: string; + Pragma: string; + 'Sec-WebSocket-Extensions': string; + 'Sec-WebSocket-Key': string; + 'Sec-WebSocket-Version': string; + Upgrade: string; + 'User-Agent': string; +} + +/** + * @name WebSocket Server + * @description + * This plugin allows you to run a single, lightweight, barebone WebSocket Server. + * + * @usage + * ```typescript + * import { WebSocketServer } from '@ionic-native/web-socket-server'; + * + * constructor(private wsserver: WebSocketServer) { } + * + * ... + * + * // start websocket server + * this.wsserver.start(8888, {}).subscribe({ + * next: server => console.log(`Listening on ${server.addr}:${server.port}`), + * error: error => console.log(`Unexpected error`, error); + * }); + * + * // watch for any messages + * this.wsserver.watchMessage().subscribe(result => { + * console.log(`Received message ${result.msg} from ${result.conn.uuid}`); + * }); + * + * // send message to connection with specified uuid + * this.wsserver.send({ uuid: '8e7c4f48-de68-4b6f-8fca-1067a353968d' }, 'Hello World'); + * + * // stop websocket server + * this.wsserver.stop().then(server => { + * console.log(`Stop listening on ${server.addr}:${server.port}`); + * }); + * + * ``` + */ +@Plugin({ + pluginName: 'WebSocketServer', + plugin: 'cordova-plugin-websocket-server', + pluginRef: 'cordova.plugins.wsserver', + repo: 'https://github.com/becvert/cordova-plugin-websocket-server', + platforms: ['Android', 'iOS'] +}) +@Injectable() +export class WebSocketServer extends IonicNativePlugin { + + /** + * Return this device's interfaces + * @return {Promise} + */ + @Cordova() + getInterfaces(): Promise { + return; + } + + /** + * Start websocket server + * @param port {number} Local port on which the service runs. (0 means any free port) + * @param options {WebSocketOptions} Additional options for websocket + * @return {Observable} Returns Observable where all generic error can be catched (mostly JSONExceptions) + */ + @Cordova({ + observable: true, + clearFunction: 'stop' + }) + start(port: number, options: WebSocketOptions): Observable { + return; + } + + private onFunctionToObservable(fnName: string) { + return new Observable(observer => { + const id = window.cordova.plugins.wsserver[fnName]( + observer.next.bind(observer), + observer.error.bind(observer) + ); + + return () => window.cordova.plugins.wsserver.removeCallback(id); + }); + } + + /** + * Watches for new messages + * @return {Observable} + */ + watchMessage(): Observable { + return this.onFunctionToObservable('onMessage'); + } + + /** + * Watches for new opened connections + * @return {Observable} + */ + watchOpen(): Observable { + return this.onFunctionToObservable('onOpen'); + } + + /** + * Watches for closed connections + * @return {Observable} + */ + watchClose(): Observable { + return this.onFunctionToObservable('onClose'); + } + + /** + * Watches for any websocket failures + * @return {Observable} + */ + watchFailure(): Observable { + return this.onFunctionToObservable('onFailure'); + } + + /** + * Stop websocket server and closes all connections + * @return {Promise} + */ + @Cordova() + stop(): Promise { + return; + } + + /** + * Send Message to a connected device + * @param conn {WebSocketIdentifier} Connection to send message to + * @param msg {string} Message to send + * @return {Promise} + */ + @Cordova() + send(conn: WebSocketIdentifier, msg: string): Promise { + return; + } + + /** + * Close specific connection using uuid + * @param conn {WebSocketIdentifier} Connection to close + * @param code {number} Close code, determines if it was clean + * @param reason {string} Reason for closing + * @return {Promise} + */ + @Cordova() + close(conn: WebSocketIdentifier, code: number, reason: string): Promise { + return; + } +}