mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 04:57:52 +00:00
Added chat integration with socket.io
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { Routes, RouterModule } from '@angular/router';
|
||||
|
||||
import { ChatPage } from './chat.page';
|
||||
|
||||
const routes: Routes = [
|
||||
{
|
||||
path: '',
|
||||
component: ChatPage
|
||||
}
|
||||
];
|
||||
|
||||
@NgModule({
|
||||
imports: [RouterModule.forChild(routes)],
|
||||
exports: [RouterModule],
|
||||
})
|
||||
export class ChatPageRoutingModule {}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
|
||||
import { IonicModule } from '@ionic/angular';
|
||||
|
||||
import { ChatPageRoutingModule } from './chat-routing.module';
|
||||
|
||||
import { ChatPage } from './chat.page';
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
CommonModule,
|
||||
FormsModule,
|
||||
IonicModule,
|
||||
ChatPageRoutingModule
|
||||
],
|
||||
declarations: [ChatPage]
|
||||
})
|
||||
export class ChatPageModule {}
|
||||
@@ -0,0 +1,39 @@
|
||||
<ion-header>
|
||||
<ion-toolbar>
|
||||
<ion-title>chat</ion-title>
|
||||
</ion-toolbar>
|
||||
</ion-header>
|
||||
|
||||
<ion-content>
|
||||
<ion-row *ngFor="let message of messages" class="chat-container">
|
||||
<ion-col size="9" *ngIf="message.user !== currentUser" class="message other-message">
|
||||
<p><b>{{message.user}}</b></p>
|
||||
<span>{{message.msg}}</span>
|
||||
<div class="message-date">{{message.createdAt | date: 'HH:mm'}}</div>
|
||||
</ion-col>
|
||||
<ion-col offset="3" size="9" *ngIf="message.user === currentUser" class="message my-message">
|
||||
<ion-label>
|
||||
<p><b>{{message.user}}</b></p>
|
||||
<span>{{message.msg}}</span>
|
||||
<div class="message-date">{{message.createdAt | date: 'HH:mm'}}</div>
|
||||
</ion-label>
|
||||
</ion-col>
|
||||
|
||||
</ion-row>
|
||||
|
||||
</ion-content>
|
||||
<ion-footer class="ion-no-border">
|
||||
<ion-toolbar>
|
||||
<ion-row align-items-center>
|
||||
<ion-col size="10">
|
||||
<ion-textarea auto-grow class="message-input" rows="1" [(ngModel)]="message"></ion-textarea>
|
||||
</ion-col>
|
||||
<ion-col size="2">
|
||||
<ion-button expand="block" fill="clear" color="primary" [disabled]="message === ''" class="msg-btn"
|
||||
(click)="sendMessage()">
|
||||
<ion-icon name="send" slot="icon-only"></ion-icon>
|
||||
</ion-button>
|
||||
</ion-col>
|
||||
</ion-row>
|
||||
</ion-toolbar>
|
||||
</ion-footer>
|
||||
@@ -0,0 +1,50 @@
|
||||
ion-content{
|
||||
background-color:#fefefe;
|
||||
}
|
||||
.chat-container{
|
||||
margin: 10px 10px 0 10px;
|
||||
}
|
||||
.message {
|
||||
padding: 5px 5px 5px 10px;
|
||||
border-radius: 10px;
|
||||
margin-bottom: 10px;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
.my-message {
|
||||
background: var(--ion-color-primary);
|
||||
color: #fff;
|
||||
}
|
||||
.my-message p{
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
color: #9ab8e9;
|
||||
}
|
||||
.message-date{
|
||||
font-size: 10px;
|
||||
text-align: right;
|
||||
margin-right: 5px;
|
||||
}
|
||||
|
||||
.other-message {
|
||||
background: #f2f3f7;
|
||||
color: #333;
|
||||
}
|
||||
.other-message p{
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
color:#959ba7;
|
||||
}
|
||||
|
||||
.message-input {
|
||||
margin-top: 0px;
|
||||
border: 1px solid var(--ion-color-medium);
|
||||
border-radius: 20px;
|
||||
background: #fff;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
import { IonicModule } from '@ionic/angular';
|
||||
|
||||
import { ChatPage } from './chat.page';
|
||||
|
||||
describe('ChatPage', () => {
|
||||
let component: ChatPage;
|
||||
let fixture: ComponentFixture<ChatPage>;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [ ChatPage ],
|
||||
imports: [IonicModule.forRoot()]
|
||||
}).compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(ChatPage);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
}));
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user