Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame] | 1 | /// <reference path="../../../../typings/index.d.ts"/> |
| 2 | |
| 3 | import {Injectable} from '@angular/core'; |
Matteo Scandolo | 40f8fa9 | 2016-12-07 09:21:35 -0800 | [diff] [blame] | 4 | import {Subject} from 'rxjs/Rx'; |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame] | 5 | import * as io from 'socket.io-client'; |
| 6 | import {AppConfig} from '../../config/app.config'; |
| 7 | import {IWSEvent} from '../../interfaces/ws.interface'; |
| 8 | |
| 9 | @Injectable() |
| 10 | export class GlobalEvent { |
Matteo Scandolo | 40f8fa9 | 2016-12-07 09:21:35 -0800 | [diff] [blame] | 11 | private _events: Subject<IWSEvent> = new Subject<IWSEvent>(); |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame] | 12 | private socket; |
| 13 | constructor() { |
| 14 | this.socket = io(AppConfig.websocketClient); |
Matteo Scandolo | 40f8fa9 | 2016-12-07 09:21:35 -0800 | [diff] [blame] | 15 | this.socket.on('event', (data: IWSEvent): void => { |
| 16 | console.log('event', data); |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame] | 17 | this._events.next(data); |
| 18 | }); |
| 19 | } |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame] | 20 | list() { |
| 21 | return this._events.asObservable(); |
| 22 | } |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame] | 23 | } |