Skip to content
Snippets Groups Projects
Commit 1170b1b6 authored by alec.schmidt's avatar alec.schmidt
Browse files

CHATROOOOOM

parent 01ba9654
Branches
No related tags found
No related merge requests found
Showing
with 97 additions and 65 deletions
No preview for this file type
......@@ -30,8 +30,10 @@ class ServerIO extends IO.Server {
}
private registerEventsOnSocket(socket: IO.Socket) {
socket.on('Hello World', _ => {
socket.emit('Bienvenue', socket.id);
socket.on('Chat', message => {
const token = socket.handshake.query.Authorization as string;
const username = this.decodeToken(token).username;
this.emit('New Message', {msg: message, sender: username});
});
socket.on('Join Room', _ => {
......
......@@ -26,3 +26,8 @@ export type Answer = {
export type Category = {
category: string;
};
export type Message = {
msg: string;
sender: string;
}
\ No newline at end of file
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
const routes: Routes = [];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class AdminRoutingModule { }
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AdminRoutingModule } from './admin-routing.module';
@NgModule({
declarations: [],
imports: [
CommonModule,
AdminRoutingModule
]
})
export class AdminModule { }
......@@ -21,6 +21,7 @@ import { DeletionConfirmationComponent } from './deletion-confirmation/deletion-
import { RemoveCategoryComponent } from './question-shit/remove-category/remove-category.component';
import { SocketIoConfig, SocketIoModule } from 'ngx-socket-io';
import { GameroomComponent } from './gameroom/gameroom.component';
import { ChatroomComponent } from './chatroom/chatroom.component';
const config: SocketIoConfig = { url: 'http://0.0.0.0:30992', options: { autoConnect: false } };
......@@ -40,7 +41,8 @@ const config: SocketIoConfig = { url: 'http://0.0.0.0:30992', options: { autoCon
CreateCategoryComponent,
DeletionConfirmationComponent,
RemoveCategoryComponent,
GameroomComponent
GameroomComponent,
ChatroomComponent
],
imports: [
BrowserModule,
......
<div class="bg-white border-black border-2 rounded-lg p-2 w-[75vw] h-96">
<h1>Chat :</h1>
<li *ngFor="let msg of chatlog" class="list-none">
<span>{{msg.sender}}</span>: <span>{{msg.msg}}</span>
</li>
<form [formGroup]="chatForm" (ngSubmit)="onSubmit()" class="bottom-0">
<input class="border-b-4 mt-2" type="text" placeholder="Your message" formControlName="message">
<button class="border-2 border-black rounded-lg px-2 hover:bg-gray-300 mt-2" type="submit">Send</button>
</form>
</div>
\ No newline at end of file
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ChatroomComponent } from './chatroom.component';
describe('ChatroomComponent', () => {
let component: ChatroomComponent;
let fixture: ComponentFixture<ChatroomComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ChatroomComponent]
});
fixture = TestBed.createComponent(ChatroomComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
import { Component, OnInit } from '@angular/core';
import { SocketService } from '../services/socket.service';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { Message } from '../Types/types';
@Component({
selector: 'app-chatroom',
templateUrl: './chatroom.component.html'
})
export class ChatroomComponent implements OnInit {
chatlog: Message[] = [];
chatForm = new FormGroup({
message: new FormControl('', Validators.required)
});
constructor(private socket: SocketService) {}
ngOnInit(): void {
this.socket.message.subscribe(chat => {
this.chatlog.push(chat as Message);
})
}
onSubmit(): void {
const message = this.chatForm.get('message')!.value;
this.chatForm.controls.message.setValue("");
this.socket.sendMessage(message);
}
}
......@@ -34,4 +34,7 @@
</table>
<button class="bg-white w-fit border-2 border-black rounded-lg px-2 hover:bg-gray-300 mt-2" (click)="exitGame()">Close</button>
</div>
<app-chatroom *ngIf="inRoom" class="absolute bottom-0 mb-10"></app-chatroom>
</div>
\ No newline at end of file
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LoginComponent } from './login.component';
const routes: Routes = [
{
path: '',
component: LoginComponent
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class LoginRoutingModule { }
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LoginRoutingModule } from './login-routing.module';
@NgModule({
declarations: [
],
imports: [
CommonModule,
LoginRoutingModule
]
})
export class LoginModule { }
import { Injectable } from '@angular/core';
import { Socket } from 'ngx-socket-io';
import { AuthenticationService } from './authentication.service';
import { BehaviorSubject } from 'rxjs';
import { QuestionsService } from './questions.service';
import { Answer, Question } from '../Types/types';
import { HttpClient } from '@angular/common/http';
import { Answer, Message } from '../Types/types';
const ROUTE = "http://0.0.0.0:30992/API/v1"
@Injectable({
......@@ -18,6 +14,7 @@ export class SocketService {
private _currentQuestion = new BehaviorSubject<string>("");
private _currentAnswers = new BehaviorSubject<Answer[]>([]);
private _points = new BehaviorSubject<number>(0);
private _message = new BehaviorSubject<Message>({msg:"", sender:""});
private _scoreboard = new BehaviorSubject<{ [username: string] : number; }>({});
constructor(
......@@ -57,6 +54,10 @@ export class SocketService {
return this._scoreboard.asObservable();
}
get message() {
return this._message.asObservable();
}
refreshSocketToken(): void {
const {token} = localStorage;
this.socket.ioSocket.io.opts.query = { Authorization: token};
......@@ -64,10 +65,12 @@ export class SocketService {
joinRoom(): void {
this.socket.emit("Join Room");
this.registerChatroom();
}
leaveRoom(): void {
this.socket.emit("Leave Room");
this.unregisterChatRoom();
}
recieveGameStatus() {
......@@ -100,12 +103,22 @@ export class SocketService {
})
}
registerChatroom(): void {
this.socket.on("New Message", data => {
this._message.next(data);
})
}
unregisterChatRoom(): void {
this.socket.off("New Message");
}
disconnectSocket() {
this.socket.disconnect();
}
sendMessage(text: string): void {
this.socket.emit("chat", text);
this.socket.emit("Chat", text);
}
sendAnswer(answer: Answer): void {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment