first commit

This commit is contained in:
wilfried 2021-05-27 10:53:34 +02:00
parent 6984c550fb
commit 595d0e3013
47 changed files with 13471 additions and 0 deletions

View file

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { ChatService } from './chat.service';
describe('ChatService', () => {
let service: ChatService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ChatService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View file

@ -0,0 +1,47 @@
import { Injectable } from '@angular/core';
import { io, Socket } from "socket.io-client";
import {Observable} from "rxjs";
@Injectable({
providedIn: 'root'
})
export class ChatService {
private socket: Socket;
private url = 'http://localhost:3000';
constructor() {
this.socket = io(this.url);
}
// @ts-ignore
joinRoom(data): void {
this.socket.emit('join', data);
}
// @ts-ignore
sendMessage(data): void{
this.socket.emit('message', data);
}
getMessage(): Observable<any> {
return new Observable<{user: string, message: string}>(observer => {
this.socket.on('new message', (data) => {
observer.next(data);
});
return () => {
this.socket.disconnect();
}
});
}
getStorage() {
const storage = localStorage.getItem('chats');
return storage ? JSON.parse(storage) : [];
}
setStorage(data: any) {
localStorage.setItem('chats', JSON.stringify(data));
}
}