Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 4th send return mode: Old Observable mode AND option auto connect code #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 50 additions & 7 deletions README.md
Expand Up @@ -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);
Expand All @@ -26,7 +26,7 @@ npm run compile
```

## example
```ts
```typescript
import {$WebSocket, WebSocketSendMode} from 'angular2-websocket/angular2-websocket';

// connect
Expand Down Expand Up @@ -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);
Expand All @@ -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
```


```
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 <code>mustReconnectCloseStatusCodeList</code> and <code>notReconnectCloseStatusCodeList</code> to set how to reconnect.
you can see the implement code as follow :
```javascript
/**
* when close code in <code>mustReconnectCloseStatusCodeList</code>, reconnect
* else,
* when <code>true==autoReconnect</code>
* AND code not in <code>notReconnectCloseStatusCodeList</code>, reconnect
* else not reconnect
*
* so, if code in <code>mustReconnectCloseStatusCodeList</code>, it always reconnect
* else if code in <code>notReconnectCloseStatusCodeList</code>, it always not reconnect
* other case see <code>autoReconnect</code>
*
* Be careful!!! if you set <code>true==autoReconnect</code>
* but <code>notReconnectCloseStatusCodeList</code> is empty,
* it will always auto connect.
* So, by default, always keep <code>notReconnectCloseStatusCodeList</code> have item <code>1000</code>
*/
if (
(
autoReconnect &&
notReconnectCloseStatusCodeList.indexOf(code) == -1
)
|| mustReconnectCloseStatusCodeList.indexOf(code) > -1
) {
// reconnect
} else {
// complete
}
```
33 changes: 30 additions & 3 deletions angular2-websocket.d.ts
Expand Up @@ -12,8 +12,6 @@ export declare class $WebSocket {
private onErrorCallbacks;
private onCloseCallbacks;
private readyStateConstants;
private normalCloseCode;
private reconnectableStatusCodes;
private socket;
private dataStream;
private internalConnectionState;
Expand Down Expand Up @@ -42,6 +40,16 @@ export declare class $WebSocket {
* @returns {Observable<any>}
*/
send4Observable(data: any): Observable<any>;
/**
* 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<any>}
*/
send4OldObservable(data: any): Observable<any>;
private send4Mode;
/**
* Set send(data) function return mode
Expand All @@ -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 <code>mustReconnectCloseStatusCodeList</code>, reconnect
* else,
* when <code>true==autoReconnect</code>
* AND code not in <code>notReconnectCloseStatusCodeList</code>, reconnect
* else not reconnect
*
* so, if code in <code>mustReconnectCloseStatusCodeList</code>, it always reconnect
* else if code in <code>notReconnectCloseStatusCodeList</code>, it always not reconnect
* other case see <code>autoReconnect</code>
*
* Be careful!!! if you set <code>true==autoReconnect</code>
* but <code>notReconnectCloseStatusCodeList</code> is empty,
* it will always auto connect.
* So, by default, always keep <code>notReconnectCloseStatusCodeList</code> have item <code>1000</code>
*/
onCloseHandler(event: CloseEvent): void;
onErrorHandler(event: any): void;
reconnect(): this;
Expand All @@ -82,10 +106,13 @@ export declare class $WebSocket {
export interface WebSocketConfig {
initialTimeout: number;
maxTimeout: number;
reconnectIfNotNormalClose: boolean;
autoReconnect: boolean;
notReconnectCloseStatusCodeList: Array<number>;
mustReconnectCloseStatusCodeList: Array<number>;
}
export declare enum WebSocketSendMode {
Direct = 0,
Promise = 1,
Observable = 2,
OldObservable = 3,
}
2 changes: 1 addition & 1 deletion 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",
Expand Down
62 changes: 55 additions & 7 deletions src/angular2-websocket.ts
Expand Up @@ -36,8 +36,6 @@ export class $WebSocket {
'CLOSED': 3,
'RECONNECT_ABORTED': 4
};
private normalCloseCode = 1000;
private reconnectableStatusCodes = [4000];
private socket: WebSocket;
private dataStream: Subject<any>;
private internalConnectionState: number;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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<any>}
*/
send4OldObservable(data): Observable<any> {
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;

/**
Expand All @@ -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.");
}
Expand Down Expand Up @@ -246,10 +271,31 @@ export class $WebSocket {
}
};

/**
* when close code in <code>mustReconnectCloseStatusCodeList</code>, reconnect
* else,
* when <code>true==autoReconnect</code>
* AND code not in <code>notReconnectCloseStatusCodeList</code>, reconnect
* else not reconnect
*
* so, if code in <code>mustReconnectCloseStatusCodeList</code>, it always reconnect
* else if code in <code>notReconnectCloseStatusCodeList</code>, it always not reconnect
* other case see <code>autoReconnect</code>
*
* Be careful!!! if you set <code>true==autoReconnect</code>
* but <code>notReconnectCloseStatusCodeList</code> is empty,
* it will always auto connect.
* So, by default, always keep <code>notReconnectCloseStatusCodeList</code> have item <code>1000</code>
*/
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 = [];
Expand Down Expand Up @@ -313,10 +359,12 @@ export class $WebSocket {
export interface WebSocketConfig {
initialTimeout: number;
maxTimeout: number;
reconnectIfNotNormalClose: boolean;
autoReconnect: boolean;
notReconnectCloseStatusCodeList: Array<number>;
mustReconnectCloseStatusCodeList: Array<number>;
}

export enum WebSocketSendMode {
Direct, Promise, Observable
Direct, Promise, Observable, OldObservable
}