Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 1 | import * as io from 'socket.io-client'; |
| 2 | import {Subject, Observable} from 'rxjs/Rx'; |
| 3 | import {AppConfig} from '../../config/app.config'; |
| 4 | |
| 5 | export interface IWSEvent { |
| 6 | model: string; |
| 7 | msg: { |
| 8 | changed_fields: string[], |
| 9 | object?: any, |
| 10 | pk?: number |
| 11 | }; |
| 12 | } |
| 13 | |
| 14 | export interface IWSEventService { |
| 15 | list(): Observable<IWSEvent>; |
| 16 | } |
| 17 | |
| 18 | export class WebSocketEvent { |
| 19 | private _events: Subject<IWSEvent> = new Subject<IWSEvent>(); |
| 20 | private socket; |
| 21 | constructor() { |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 22 | this.socket = io(AppConfig.websocketClient); |
| 23 | this.socket.on('event', (data: IWSEvent): void => { |
| 24 | this._events.next(data); |
| 25 | }); |
| 26 | } |
| 27 | list() { |
| 28 | return this._events.asObservable(); |
| 29 | } |
| 30 | } |