Skip to content

Commit

Permalink
fix issues #78 (#83)
Browse files Browse the repository at this point in the history
* fix wrong dataStream behaviour after reconnect

* change d.ts
  • Loading branch information
godzie44 authored and afrad committed Jun 12, 2017
1 parent 9c02bea commit 6aadf98
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions angular2-websocket.d.ts
Expand Up @@ -59,6 +59,7 @@ export declare class $WebSocket {
*/
send(data: any, mode?: WebSocketSendMode, binary?: boolean): any;
getDataStream(): Subject<any>;
getErrorStream(): Subject<any>;
onOpenHandler(event: Event): void;
notifyOpenCallbacks(event: any): void;
fireQueue(): void;
Expand Down
30 changes: 18 additions & 12 deletions src/angular2-websocket.ts
Expand Up @@ -40,6 +40,7 @@ export class $WebSocket {
private reconnectableStatusCodes = [4000];
private socket: WebSocket;
private dataStream: Subject<any>;
private errorMessages: Subject<any>;
private internalConnectionState: number;

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

Expand Down Expand Up @@ -77,12 +79,16 @@ export class $WebSocket {
this.socket.onerror = (ev: ErrorEvent) => {
// console.log('onError ', ev);
self.onErrorHandler(ev);
this.dataStream.error(ev);
this.errorMessages.next(ev);
};

}
}

getErrorStream(): Subject<any> {
return this.errorMessages;
}

/**
* Run in Block Mode
* Return true when can send and false in socket closed
Expand All @@ -92,15 +98,15 @@ export class $WebSocket {
send4Direct(data, binary?: boolean): boolean {
let self = this;
if (this.getReadyState() !== this.readyStateConstants.OPEN
&& this.getReadyState() !== this.readyStateConstants.CONNECTING) {
&& this.getReadyState() !== this.readyStateConstants.CONNECTING) {
this.connect();
}
self.sendQueue.push({message: data, binary: binary});
if (self.socket.readyState === self.readyStateConstants.OPEN) {
self.fireQueue();
return true;
} else {
return false;
return false;
}
}

Expand All @@ -113,13 +119,13 @@ export class $WebSocket {
*/
send4Promise(data, binary?: boolean): Promise<any> {
return new Promise(
(resolve, reject) => {
if (this.send4Direct(data, binary)) {
return resolve();
} else {
return reject(Error('Socket connection has been closed'));
}
}
(resolve, reject) => {
if (this.send4Direct(data, binary)) {
return resolve();
} else {
return reject(Error('Socket connection has been closed'));
}
}
)
}

Expand Down Expand Up @@ -197,7 +203,7 @@ export class $WebSocket {
this.socket.send(data.message);
} else {
this.socket.send(
$WebSocket.Helpers.isString(data.message) ? data.message : JSON.stringify(data.message)
$WebSocket.Helpers.isString(data.message) ? data.message : JSON.stringify(data.message)
);
}
// data.deferred.resolve();
Expand Down Expand Up @@ -256,7 +262,7 @@ export class $WebSocket {
onCloseHandler(event: CloseEvent) {
this.notifyCloseCallbacks(event);
if ((this.config.reconnectIfNotNormalClose && event.code !== this.normalCloseCode)
|| this.reconnectableStatusCodes.indexOf(event.code) > -1) {
|| this.reconnectableStatusCodes.indexOf(event.code) > -1) {
this.reconnect();
} else {
this.sendQueue = [];
Expand Down

0 comments on commit 6aadf98

Please sign in to comment.