Skip to content

Commit 908aa55

Browse files
Lyoko-Jeremieafrad
authored andcommitted
fix send, fix lost data before connect open, add example. (#49)
* change send returning from cold Observable to 3 mode 1(Direct) send and return true or false (false when socket is close) 2(Promise) send and return promise 3(Observable) send and return cold Observable the default mode is 1(Direct). and as soon as that, import bluebird * change send returning from cold Observable to 3 mode 1(Direct) send and return true or false (false when socket is close) 2(Promise) send and return promise 3(Observable) send and return cold Observable the default mode is 3(Observable). and as soon as that, update compile target to es6 and import bluebird * update typescript version to 2.1.1 to fix typescript server compile error on webstorm * fix * fix data lose when connect creating before connect open. now you can connect and immediately send data! the data will cache in local until connect create or lost, cached data will send in connect create immediately! every connect close in not reconnect mode will clear cached data, but reconnect will not. * enum change name * add example * small change
1 parent c2c93b8 commit 908aa55

File tree

5 files changed

+253
-43
lines changed

5 files changed

+253
-43
lines changed

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,86 @@ ws.getDataStream() returns Subject<any> to which you can attach an Observer (htt
2323
npm run typings
2424
npm run compile
2525

26+
```
27+
28+
## example
29+
```ts
30+
import {$WebSocket, WebSocketSendMode} from 'angular2-websocket/angular2-websocket';
31+
32+
// connect
33+
var ws = new $WebSocket("ws://127.0.0.1:7000");
34+
// you can send immediately after connect,
35+
// data will cached until connect open and immediately send or connect fail.
36+
37+
// when connect fail, websocket will reconnect or not,
38+
// you can set {WebSocketConfig.reconnectIfNotNormalClose = true} to enable auto reconnect
39+
// all cached data will lost when connect close if not reconnect
40+
41+
42+
// set received message callback
43+
ws.onMessage(
44+
(msg: MessageEvent)=> {
45+
console.log("onMessage ", msg.data);
46+
},
47+
{autoApply: false}
48+
);
49+
50+
// set received message stream
51+
ws.getDataStream().subscribe(
52+
(msg)=> {
53+
console.log("next", msg.data);
54+
ws.close(false);
55+
},
56+
(msg)=> {
57+
console.log("error", msg);
58+
},
59+
()=> {
60+
console.log("complete");
61+
}
62+
);
63+
64+
// send with default send mode (now default send mode is Observer)
65+
ws.send("some thing").subscribe(
66+
(msg)=> {
67+
console.log("next", msg.data);
68+
},
69+
(msg)=> {
70+
console.log("error", msg);
71+
},
72+
()=> {
73+
console.log("complete");
74+
}
75+
);
76+
77+
ws.send("by default, this will never be send, because Observer is cold.");
78+
ws.send("by default, this will be send, because Observer is hot.").publish().connect();
79+
80+
ws.setSendMode(WebSocketSendMode.Direct);
81+
ws.send("this will be send Direct, because send mode is set to Direct.");
82+
83+
ws.send("this will be send and return Promise.", WebSocketSendMode.Promise).then(
84+
(T) => {
85+
console.log("is send");
86+
},
87+
(T) => {
88+
console.log("not send");
89+
}
90+
);
91+
92+
ws.send("this will be send and return Observer.").subscribe(
93+
(msg)=> {
94+
console.log("next", msg.data);
95+
},
96+
(msg)=> {
97+
console.log("error", msg);
98+
},
99+
()=> {
100+
console.log("complete");
101+
}
102+
);
103+
104+
ws.close(false); // close
105+
ws.close(true); // close immediately
106+
107+
26108
```

angular2-websocket.d.ts

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import { Observable } from "rxjs/Observable";
2-
import { Subject } from "rxjs/Subject";
3-
export interface WebSocketConfig {
4-
initialTimeout: number;
5-
maxTimeout: number;
6-
reconnectIfNotNormalClose: boolean;
7-
}
1+
import { Observable } from 'rxjs/Observable';
2+
import { Subject } from 'rxjs/Subject';
83
export declare class $WebSocket {
94
private url;
105
private protocols;
116
private config;
7+
private static Helpers;
128
private reconnectAttempts;
139
private sendQueue;
1410
private onOpenCallbacks;
@@ -19,10 +15,48 @@ export declare class $WebSocket {
1915
private normalCloseCode;
2016
private reconnectableStatusCodes;
2117
private socket;
18+
private dataStream;
2219
private internalConnectionState;
2320
constructor(url: string, protocols?: Array<string>, config?: WebSocketConfig);
2421
connect(force?: boolean): void;
25-
static create(url: string, protocols: Array<string>): WebSocket;
22+
/**
23+
* Run in Block Mode
24+
* Return true when can send and false in socket closed
25+
* @param data
26+
* @returns {boolean}
27+
*/
28+
send4Direct(data: any): boolean;
29+
/**
30+
* Return Promise
31+
* When can Send will resolve Promise
32+
* When Socket closed will reject Promise
33+
* @param data
34+
* @returns {Promise<any>}
35+
*/
36+
send4Promise(data: any): Promise<any>;
37+
/**
38+
* Return cold Observable
39+
* When can Send will complete observer
40+
* When Socket closed will error observer
41+
* @param data
42+
* @returns {Observable<any>}
43+
*/
44+
send4Observable(data: any): Observable<any>;
45+
private send4Mode;
46+
/**
47+
* Set send(data) function return mode
48+
* @param mode
49+
*/
50+
setSend4Mode(mode: WebSocketSendMode): void;
51+
/**
52+
* Use {mode} mode to send {data} data
53+
* If no specify, Default SendMode is Observable mode
54+
* @param data
55+
* @param mode
56+
* @returns {any}
57+
*/
58+
send(data: any, mode?: WebSocketSendMode): any;
59+
getDataStream(): Subject<any>;
2660
onOpenHandler(event: Event): void;
2761
notifyOpenCallbacks(event: any): void;
2862
fireQueue(): void;
@@ -31,16 +65,27 @@ export declare class $WebSocket {
3165
onOpen(cb: any): this;
3266
onClose(cb: any): this;
3367
onError(cb: any): this;
34-
onMessage(callback: any, options: any): this;
68+
onMessage(callback: any, options?: any): this;
3569
onMessageHandler(message: MessageEvent): void;
3670
onCloseHandler(event: CloseEvent): void;
3771
onErrorHandler(event: any): void;
38-
send(data: any): Observable<any>;
3972
reconnect(): this;
40-
close(force: boolean): this;
73+
close(force?: boolean): this;
4174
getBackoffDelay(attempt: any): number;
4275
setInternalState(state: any): void;
76+
/**
77+
* Could be -1 if not initzialized yet
78+
* @returns {number}
79+
*/
4380
getReadyState(): number;
44-
setReadyState(): void;
45-
getDataStream(): Subject<any>;
81+
}
82+
export interface WebSocketConfig {
83+
initialTimeout: number;
84+
maxTimeout: number;
85+
reconnectIfNotNormalClose: boolean;
86+
}
87+
export declare enum WebSocketSendMode {
88+
Direct = 0,
89+
Promise = 1,
90+
Observable = 2,
4691
}

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular2-websocket",
33
"main": "angular2-websocket",
4-
"version": "0.8.1",
4+
"version": "0.9.0",
55
"scripts": {
66
"test": "echo \"Error: no test specified\" && exit 1",
77
"compile": "tsc",
@@ -10,24 +10,27 @@
1010
},
1111
"repository": {
1212
"type": "git",
13-
"url": "git+https://github.com/afrad/angular2-websocket.git"
13+
"url": "git+https://github.com/Lyoko-Jeremie/angular2-websocket.git"
1414
},
1515
"dependencies": {
1616
"@angular/common": "~2.0.0",
1717
"@angular/compiler": "~2.0.0",
1818
"@angular/core": "~2.0.0",
19+
"bluebird": "^3.4.6",
1920
"core-js": "^2.4.1",
2021
"reflect-metadata": "^0.1.8",
2122
"rxjs": "5.0.0-beta.12",
23+
"typescript": "^2.1.1",
2224
"zone.js": "^0.6.25"
2325
},
2426
"devDependencies": {
27+
"@types/bluebird": "^3.0.35",
2528
"codelyzer": "^1.0.0-beta.0",
2629
"grunt": "~0.4.1",
27-
"grunt-typescript": "~0.2.4",
2830
"grunt-contrib-uglify": "~0.2.4",
31+
"grunt-typescript": "~0.2.4",
2932
"tslint": "^3.15.1",
30-
"typings": "^1.4.0",
31-
"typescript": "^2.0.3"
33+
"typescript": "^2.0.3",
34+
"typings": "^1.4.0"
3235
}
3336
}

0 commit comments

Comments
 (0)