Skip to content

Commit 6aadf98

Browse files
godzie44afrad
authored andcommitted
fix issues #78 (#83)
* fix wrong dataStream behaviour after reconnect * change d.ts
1 parent 9c02bea commit 6aadf98

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

angular2-websocket.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export declare class $WebSocket {
5959
*/
6060
send(data: any, mode?: WebSocketSendMode, binary?: boolean): any;
6161
getDataStream(): Subject<any>;
62+
getErrorStream(): Subject<any>;
6263
onOpenHandler(event: Event): void;
6364
notifyOpenCallbacks(event: any): void;
6465
fireQueue(): void;

src/angular2-websocket.ts

+18-12
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export class $WebSocket {
4040
private reconnectableStatusCodes = [4000];
4141
private socket: WebSocket;
4242
private dataStream: Subject<any>;
43+
private errorMessages: Subject<any>;
4344
private internalConnectionState: number;
4445

4546
constructor(private url: string, private protocols?: Array<string>, private config?: WebSocketConfig, private binaryType?: BinaryType) {
@@ -50,6 +51,7 @@ export class $WebSocket {
5051
this.config = config || {initialTimeout: 500, maxTimeout: 300000, reconnectIfNotNormalClose: false};
5152
this.binaryType = binaryType || "blob";
5253
this.dataStream = new Subject();
54+
this.errorMessages = new Subject();
5355
this.connect(true);
5456
}
5557

@@ -77,12 +79,16 @@ export class $WebSocket {
7779
this.socket.onerror = (ev: ErrorEvent) => {
7880
// console.log('onError ', ev);
7981
self.onErrorHandler(ev);
80-
this.dataStream.error(ev);
82+
this.errorMessages.next(ev);
8183
};
8284

8385
}
8486
}
8587

88+
getErrorStream(): Subject<any> {
89+
return this.errorMessages;
90+
}
91+
8692
/**
8793
* Run in Block Mode
8894
* Return true when can send and false in socket closed
@@ -92,15 +98,15 @@ export class $WebSocket {
9298
send4Direct(data, binary?: boolean): boolean {
9399
let self = this;
94100
if (this.getReadyState() !== this.readyStateConstants.OPEN
95-
&& this.getReadyState() !== this.readyStateConstants.CONNECTING) {
101+
&& this.getReadyState() !== this.readyStateConstants.CONNECTING) {
96102
this.connect();
97103
}
98104
self.sendQueue.push({message: data, binary: binary});
99105
if (self.socket.readyState === self.readyStateConstants.OPEN) {
100106
self.fireQueue();
101107
return true;
102108
} else {
103-
return false;
109+
return false;
104110
}
105111
}
106112

@@ -113,13 +119,13 @@ export class $WebSocket {
113119
*/
114120
send4Promise(data, binary?: boolean): Promise<any> {
115121
return new Promise(
116-
(resolve, reject) => {
117-
if (this.send4Direct(data, binary)) {
118-
return resolve();
119-
} else {
120-
return reject(Error('Socket connection has been closed'));
121-
}
122-
}
122+
(resolve, reject) => {
123+
if (this.send4Direct(data, binary)) {
124+
return resolve();
125+
} else {
126+
return reject(Error('Socket connection has been closed'));
127+
}
128+
}
123129
)
124130
}
125131

@@ -197,7 +203,7 @@ export class $WebSocket {
197203
this.socket.send(data.message);
198204
} else {
199205
this.socket.send(
200-
$WebSocket.Helpers.isString(data.message) ? data.message : JSON.stringify(data.message)
206+
$WebSocket.Helpers.isString(data.message) ? data.message : JSON.stringify(data.message)
201207
);
202208
}
203209
// data.deferred.resolve();
@@ -256,7 +262,7 @@ export class $WebSocket {
256262
onCloseHandler(event: CloseEvent) {
257263
this.notifyCloseCallbacks(event);
258264
if ((this.config.reconnectIfNotNormalClose && event.code !== this.normalCloseCode)
259-
|| this.reconnectableStatusCodes.indexOf(event.code) > -1) {
265+
|| this.reconnectableStatusCodes.indexOf(event.code) > -1) {
260266
this.reconnect();
261267
} else {
262268
this.sendQueue = [];

0 commit comments

Comments
 (0)