import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs/Observable' ; import * as socketIo from 'socket.io-client' ; import { ISocketIOMessagePost , ISocketIOMessage } from '../../../../../../shared/models/messaging/ISocketIOMessage' ; import { SocketIOEvent } from '../../../../../../shared/models/messaging/SoketIOEvent' ; @ Injectable ( ) export class ChatService { private socket ; constructor ( ) { } public initSocket ( room ) { this . socket = socketIo ( { path : '/chat' , query : { room : room , cookie : 'token' } } ) ; } public send ( socketIOMessagePost ) { this . socket . emit ( SocketIOEvent . MESSAGE , socketIOMessagePost ) ; } public onMessage ( ) { return new Observable < ISocketIOMessage > ( observer => { this . socket . on ( SocketIOEvent . MESSAGE , ( data ) => observer . next ( data ) ) ; } ) ; } public onEvent ( event ) { return new Observable < Event > ( observer => { this . socket . on ( event , ( ) => observer . next ( ) ) ; } ) ; } }