diff --git a/README.md b/README.md index ef0ffe3..03505ee 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ npm install angular2-websocket ``` ## Usage: -```ts +```typescript import {$WebSocket} from 'angular2-websocket/angular2-websocket' var ws = new $WebSocket("url"); ws.send(event); @@ -26,7 +26,7 @@ npm run compile ``` ## example -```ts +```typescript import {$WebSocket, WebSocketSendMode} from 'angular2-websocket/angular2-websocket'; // connect @@ -89,9 +89,8 @@ ws.send("this will be send and return Promise.", WebSocketSendMode.Promise).then } ); -ws.send("this will be send and return Observer.").subscribe( +ws.send("this will be send and return Observer. Complete when send ok.", WebSocketSendMode.Observable).subscribe( (msg)=> { - console.log("next", msg.data); }, (msg)=> { console.log("error", msg); @@ -100,9 +99,53 @@ ws.send("this will be send and return Observer.").subscribe( console.log("complete"); } ); - + +ws.send("this will be send and return Observer. Next when send ok.", WebSocketSendMode.OldObservable).subscribe( + (msg)=> { + console.log("next", msg.data); + }, + (msg)=> { + console.log("error", msg); + } + ); + ws.close(false); // close ws.close(true); // close immediately +``` - -``` \ No newline at end of file +we have 4 mode to send and return. +you can see those different in function comment. + + +##User Define Auto Reconnect Close Code +now you can use mustReconnectCloseStatusCodeList and notReconnectCloseStatusCodeList to set how to reconnect. +you can see the implement code as follow : +```javascript +/** + * when close code in mustReconnectCloseStatusCodeList, reconnect + * else, + * when true==autoReconnect + * AND code not in notReconnectCloseStatusCodeList, reconnect + * else not reconnect + * + * so, if code in mustReconnectCloseStatusCodeList, it always reconnect + * else if code in notReconnectCloseStatusCodeList, it always not reconnect + * other case see autoReconnect + * + * Be careful!!! if you set true==autoReconnect + * but notReconnectCloseStatusCodeList is empty, + * it will always auto connect. + * So, by default, always keep notReconnectCloseStatusCodeList have item 1000 + */ +if ( + ( + autoReconnect && + notReconnectCloseStatusCodeList.indexOf(code) == -1 + ) + || mustReconnectCloseStatusCodeList.indexOf(code) > -1 +) { + // reconnect +} else { + // complete +} +``` diff --git a/angular2-websocket.d.ts b/angular2-websocket.d.ts index eb61e65..55243e9 100644 --- a/angular2-websocket.d.ts +++ b/angular2-websocket.d.ts @@ -12,8 +12,6 @@ export declare class $WebSocket { private onErrorCallbacks; private onCloseCallbacks; private readyStateConstants; - private normalCloseCode; - private reconnectableStatusCodes; private socket; private dataStream; private internalConnectionState; @@ -42,6 +40,16 @@ export declare class $WebSocket { * @returns {Observable} */ send4Observable(data: any): Observable; + /** + * Return cold Observable + * When can Send will next observer + * When Socket closed will error observer + * + * this function will useful when someone use flatMap in Rxjs + * @param data + * @returns {Observable} + */ + send4OldObservable(data: any): Observable; private send4Mode; /** * Set send(data) function return mode @@ -67,6 +75,22 @@ export declare class $WebSocket { onError(cb: any): this; onMessage(callback: any, options?: any): this; onMessageHandler(message: MessageEvent): void; + /** + * when close code in mustReconnectCloseStatusCodeList, reconnect + * else, + * when true==autoReconnect + * AND code not in notReconnectCloseStatusCodeList, reconnect + * else not reconnect + * + * so, if code in mustReconnectCloseStatusCodeList, it always reconnect + * else if code in notReconnectCloseStatusCodeList, it always not reconnect + * other case see autoReconnect + * + * Be careful!!! if you set true==autoReconnect + * but notReconnectCloseStatusCodeList is empty, + * it will always auto connect. + * So, by default, always keep notReconnectCloseStatusCodeList have item 1000 + */ onCloseHandler(event: CloseEvent): void; onErrorHandler(event: any): void; reconnect(): this; @@ -82,10 +106,13 @@ export declare class $WebSocket { export interface WebSocketConfig { initialTimeout: number; maxTimeout: number; - reconnectIfNotNormalClose: boolean; + autoReconnect: boolean; + notReconnectCloseStatusCodeList: Array; + mustReconnectCloseStatusCodeList: Array; } export declare enum WebSocketSendMode { Direct = 0, Promise = 1, Observable = 2, + OldObservable = 3, } diff --git a/package.json b/package.json index 566585d..4133914 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "angular2-websocket", "main": "angular2-websocket", - "version": "0.9.0", + "version": "0.9.1", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "compile": "tsc", diff --git a/src/angular2-websocket.ts b/src/angular2-websocket.ts index 0d98c16..d547e48 100644 --- a/src/angular2-websocket.ts +++ b/src/angular2-websocket.ts @@ -36,8 +36,6 @@ export class $WebSocket { 'CLOSED': 3, 'RECONNECT_ABORTED': 4 }; - private normalCloseCode = 1000; - private reconnectableStatusCodes = [4000]; private socket: WebSocket; private dataStream: Subject; private internalConnectionState: number; @@ -47,7 +45,13 @@ export class $WebSocket { if (!match) { throw new Error('Invalid url provided'); } - this.config = config || {initialTimeout: 500, maxTimeout: 300000, reconnectIfNotNormalClose: false}; + this.config = config || { + initialTimeout: 500, + maxTimeout: 300000, + autoReconnect: false, + notReconnectCloseStatusCodeList: [1000], + mustReconnectCloseStatusCodeList: [4000], + }; this.dataStream = new Subject(); this.connect(true); } @@ -138,6 +142,25 @@ export class $WebSocket { }); } + /** + * Return cold Observable + * When can Send will next observer + * When Socket closed will error observer + * + * this function will useful when someone use flatMap in Rxjs + * @param data + * @returns {Observable} + */ + send4OldObservable(data): Observable { + return Observable.create((observer) => { + if (this.send4Direct(data)) { + return observer.next(); + } else { + return observer.error('Socket connection has been closed'); + } + }); + } + private send4Mode: WebSocketSendMode = WebSocketSendMode.Observable; /** @@ -163,6 +186,8 @@ export class $WebSocket { return this.send4Promise(data); case WebSocketSendMode.Observable: return this.send4Observable(data); + case WebSocketSendMode.OldObservable: + return this.send4OldObservable(data); default: throw Error("WebSocketSendMode Error."); } @@ -246,10 +271,31 @@ export class $WebSocket { } }; + /** + * when close code in mustReconnectCloseStatusCodeList, reconnect + * else, + * when true==autoReconnect + * AND code not in notReconnectCloseStatusCodeList, reconnect + * else not reconnect + * + * so, if code in mustReconnectCloseStatusCodeList, it always reconnect + * else if code in notReconnectCloseStatusCodeList, it always not reconnect + * other case see autoReconnect + * + * Be careful!!! if you set true==autoReconnect + * but notReconnectCloseStatusCodeList is empty, + * it will always auto connect. + * So, by default, always keep notReconnectCloseStatusCodeList have item 1000 + */ onCloseHandler(event: CloseEvent) { this.notifyCloseCallbacks(event); - if ((this.config.reconnectIfNotNormalClose && event.code !== this.normalCloseCode) - || this.reconnectableStatusCodes.indexOf(event.code) > -1) { + if ( + ( + this.config.autoReconnect && + this.config.notReconnectCloseStatusCodeList.indexOf(event.code) == -1 + ) + || this.config.mustReconnectCloseStatusCodeList.indexOf(event.code) > -1 + ) { this.reconnect(); } else { this.sendQueue = []; @@ -313,10 +359,12 @@ export class $WebSocket { export interface WebSocketConfig { initialTimeout: number; maxTimeout: number; - reconnectIfNotNormalClose: boolean; + autoReconnect: boolean; + notReconnectCloseStatusCodeList: Array; + mustReconnectCloseStatusCodeList: Array; } export enum WebSocketSendMode { - Direct, Promise, Observable + Direct, Promise, Observable, OldObservable }