Added chat integration with socket.io

This commit is contained in:
Tiago Kayaya
2020-09-10 09:48:37 +01:00
parent b8e3623045
commit c4e52172ef
20 changed files with 410 additions and 151 deletions
+52
View File
@@ -0,0 +1,52 @@
import { Component, OnInit } from '@angular/core';
import { Socket } from 'ngx-socket-io';
import { ToastController } from '@ionic/angular';
import { WebsocketService } from 'src/app/services/websocket.service';
import { fromEvent } from 'rxjs';
@Component({
selector: 'app-chat',
templateUrl: './chat.page.html',
styleUrls: ['./chat.page.scss'],
})
export class ChatPage implements OnInit {
message = '';
messages = [];
currentUser = '';
constructor(private websocket:WebsocketService, private socket: Socket) { }
ngOnInit() {
console.log("ON");
this.socket.connect();
/* Set current user */
let name = `User-${new Date().getTime()}`;
this.currentUser=name;
this.socket.emit('set-name', name);
/* Reat from event calling "fromEvent" */
this.socket.fromEvent('users-changed').subscribe(data =>{
console.log('gOT data:', data);
});
/* Add message to the array of messages */
this.socket.fromEvent('message').subscribe(message =>{
console.log('New:', message);
this.messages.push(message);
});
}
sendMessage(){
this.socket.emit('send-message', {text: this.message});
this.message="";
}
ionViewWillLeave(){
this.socket.disconnect();
}
}