Skip to content

Commit

Permalink
feat(socketio-plugin): WebSocketConnected event now carry socketId
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlking committed Dec 22, 2018
1 parent 84832e8 commit fe61005
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions PLAYBOOK.md
Expand Up @@ -314,6 +314,7 @@ ng g lib ThemePicker --tags=public-module --publishable=true --unit-test-runner=
ng g component ThemePicker --project=theme-picker --flat -d
ng g service ThemeStorage --project=theme-picker -d
ng g service StyleManager --project=theme-picker -d
ng g module ThemePickerService --project=theme-picker --flat -d

# generate components for `Notifications` Module
ng g lib Notifications --tags=public-module --publishable=true --unit-test-runner=jest -d
Expand Down
6 changes: 4 additions & 2 deletions libs/ngx-utils/package.json
Expand Up @@ -2,7 +2,9 @@
"name": "@ngx-starter-kit/ngx-utils",
"version": "0.0.1",
"peerDependencies": {
"@angular/common": "^7.1.0",
"@angular/core": "^7.1.0"
"@angular/common": ">=6.0.0 <8.0.0",
"@angular/core": ">=6.0.0 <8.0.0",
"date-fns": "^2.0.0-alpha.26",
"rxjs": ">=6.0.0"
}
}
4 changes: 3 additions & 1 deletion libs/socketio-plugin/src/lib/RxSocketioSubject.ts
Expand Up @@ -90,7 +90,9 @@ export class RxSocketioSubject<T> extends AnonymousSubject<T> {

this._socket = config.connectOpts ? io(config.url, config.connectOpts) : io(config.url);

this._socket.on('connect', event => config.openObserver.next(event));
this._socket.on('connect', event =>
config.openObserver.next(new CustomEvent('opened', { detail: { id: this._socket.id } })),
);

this._socket.on('disconnect', event => config.closeObserver.next(event));

Expand Down
1 change: 1 addition & 0 deletions libs/socketio-plugin/src/lib/symbols.ts
Expand Up @@ -80,6 +80,7 @@ export class SendWebSocketAction {

export class WebSocketConnected {
static readonly type = '[Websocket] Connected';
constructor(public payload: { socketId: string }) {}
}

export class WebSocketDisconnected {
Expand Down
2 changes: 1 addition & 1 deletion libs/socketio-plugin/src/lib/websocket-handler.ts
Expand Up @@ -31,7 +31,7 @@ export class WebSocketHandler {
actions.pipe(ofActionDispatched(AuthenticateWebSocket)).subscribe(event => socket.auth({ sumo: 1 }));
socket.connectionStatus.pipe(distinctUntilChanged()).subscribe(status => {
if (status) {
store.dispatch(new WebSocketConnected());
store.dispatch(new WebSocketConnected({ socketId: socket.id }));
} else {
store.dispatch(new WebSocketDisconnected());
}
Expand Down
11 changes: 10 additions & 1 deletion libs/socketio-plugin/src/lib/websocket-subject.ts
Expand Up @@ -15,6 +15,10 @@ export class WebSocketSubject extends Subject<any> {
* The connection status of the websocket.
*/
connectionStatus = new Subject<boolean>();
private _id: string;
get id() {
return this._id;
}

private _socket: RxSocketioSubject<SocketIOEvent>;
private _internalConfig: RxSocketioSubjectConfig<SocketIOEvent>;
Expand All @@ -32,7 +36,12 @@ export class WebSocketSubject extends Subject<any> {
},
},
openObserver: {
next: (e: Event) => this.connectionStatus.next(true),
next: (e: CustomEvent) => {
if (e.detail.id) {
this._id = e.detail.id;
}
this.connectionStatus.next(true);
},
},
};
}
Expand Down

0 comments on commit fe61005

Please sign in to comment.