import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs' ; import * as io from 'socket.io-client' ; import { IMessage } from '../../models' ; export class SocketService { private host : string = window . location . protocol + '//' + window . location . hostname + ':' + window . location . port ; socket : SocketIOClient . Socket ; constructor ( private name ) { let socketUrl = this . host + '/' + this . name ; this . socket = io . connect ( socketUrl ) ; } items ( ) { return Observable . create ( observer => { this . socket . on ( 'item' , ( item ) => observer . next ( item ) ) ; } ) ; } list ( ) { this . socket . emit ( 'list' ) ; } create ( params ) { this . socket . emit ( 'create' , params ) ; } remove ( params ) { this . socket . emit ( 'remove' , params ) ; } onConnect ( ) { return new Observable ( observer => { this . socket . on ( 'connect' , ( ) => observer . complete ( ) ) ; } ) ; } onDisconnect ( ) { return new Observable ( observer => { this . socket . on ( 'disconnect' , ( ) => observer . complete ( ) ) ; } ) ; } }