first commit
This commit is contained in:
parent
6984c550fb
commit
595d0e3013
47 changed files with 13471 additions and 0 deletions
16
frontend/src/app/services/chat/chat.service.spec.ts
Normal file
16
frontend/src/app/services/chat/chat.service.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
47
frontend/src/app/services/chat/chat.service.ts
Normal file
47
frontend/src/app/services/chat/chat.service.ts
Normal 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));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in a new issue