Created
August 28, 2018 06:25
-
-
Save chax/89a3805b08c875d3065a8cf2fb301668 to your computer and use it in GitHub Desktop.
Typescript class CordovaWebSocket for easier handling of CordovaWebSocketPlugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Observable, Subject } from 'rxjs'; | |
declare var CordovaWebsocketPlugin: any; | |
export class CordovaWebSocket { | |
private _wsReceiveObserver: Subject<any> = new Subject<string>(); | |
private _wsCloseObserver: Subject<any> = new Subject<CordovaWebSocketResult>(); | |
private _wsOptions: CordovaWebSocketOptions; | |
private _wsId: string; | |
public isOpen: boolean = false; | |
public url: string; | |
constructor(options: CordovaWebSocketOptions) { | |
this._wsOptions = options; | |
} | |
private _handleWsEvent(event: any) { | |
let eventType = event["callbackMethod"]; | |
switch (eventType) { | |
case "onMessage": | |
this._wsReceiveObserver.next(event["message"]); | |
break; | |
case "onClose": | |
case "onFail": | |
let closeEvent: CordovaWebSocketResult = { | |
code: event["code"], | |
reason: event["reason"], | |
exception: event["exception"] | |
}; | |
this.isOpen = false; | |
this.url = ""; | |
this._wsCloseObserver.next(closeEvent); | |
break; | |
} | |
} | |
async connect(): Promise<CordovaWebSocketResult> { | |
let self = this; | |
return new Promise<CordovaWebSocketResult>((resolve, reject) => { | |
CordovaWebsocketPlugin.wsConnect(self._wsOptions, | |
ev => self._handleWsEvent(ev), | |
success => { | |
self._wsId = success.webSocketId; | |
self.isOpen = true; | |
self.url = self._wsOptions.url; | |
let result: CordovaWebSocketResult = { | |
code: success["code"] | |
} | |
resolve(result); | |
}, | |
error => { | |
self._wsId = error.webSocketId; | |
self.isOpen = false; | |
let result: CordovaWebSocketResult = { | |
code: error["code"], | |
reason: error["reason"], | |
exception: error["exception"] | |
} | |
reject(result); | |
} | |
); | |
}); | |
} | |
send(message: string) { | |
if (this.isOpen && this._wsId != undefined) { | |
CordovaWebsocketPlugin.wsSend(this._wsId, message); | |
} | |
} | |
close(code: number, reason: string) { | |
if (this.isOpen && this._wsId != undefined) { | |
if (code == undefined) { | |
code = 1000; | |
} | |
if (reason == undefined) { | |
reason = ""; | |
} | |
CordovaWebsocketPlugin.wsClose(this._wsId, code, reason); | |
} | |
} | |
public get onMessage(): Observable<string> { | |
return <Observable<any>> this._wsReceiveObserver; | |
} | |
public get onClose(): Observable<CordovaWebSocketResult> { | |
return <Observable<any>> this._wsCloseObserver; | |
} | |
} | |
export interface CordovaWebSocketResult { | |
code: number; | |
reason?: string; | |
exception?: string; | |
} | |
export interface CordovaWebSocketOptions { | |
url: string; | |
timeout?: number; | |
pingInterval?: number; | |
headers?: object; | |
acceptAllCerts?: boolean; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment