import { ReplaySubject } from 'rxjs' ; import { SocketService , UserService } from '../core' ; import { IMessage } from '../../models' ; export class MessageService { messages : ReplaySubject < any > = new ReplaySubject ( 1 ) ; private list : IMessage [ ] = [ ] ; private socketService : SocketService ; constructor ( private userService , private room ) { this . socketService = new SocketService ( 'messages/' + encodeURIComponent ( this . room ) ) ; this . socketService . items ( ) . subscribe ( message => { this . list . push ( message ) ; this . messages . next ( this . list ) ; } , error => console . log ( error ) ) ; this . socketService . onConnect ( ) . subscribe ( x => { } , e => { } , ( ) => this . send ( `${ this . userService . nickname } joined the channel` ) ) ; this . socketService . onDisconnect ( ) . subscribe ( x => { } , e => { } , ( ) => this . send ( `${ this . userService . nickname } left the channel` ) ) ; } send ( message ) { this . socketService . create ( { room : this . room , created : new Date ( ) , from : this . userService . nickname , to : '' , message : message } ) ; } leave ( ) { this . socketService . socket . disconnect ( ) ; } }