Skip to content

Commit

Permalink
update WS
Browse files Browse the repository at this point in the history
  • Loading branch information
deemakuzovkin committed Jul 9, 2023
1 parent 6d86483 commit 454cca1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 52 deletions.
4 changes: 4 additions & 0 deletions src/app/simple/simple.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ image#image1_204_3 {
font-size: 110px;
}

#username{
font-size: 60px;
}

@keyframes fadeIn {
0% {
opacity: 1;
Expand Down
16 changes: 3 additions & 13 deletions src/app/simple/simple.component.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 37 additions & 39 deletions src/app/simple/simple.component.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,77 @@
import {Component, OnInit} from '@angular/core';
import {Component, OnDestroy, OnInit} from '@angular/core';

@Component({
selector: 'app-simple',
templateUrl: './simple.component.svg',
styleUrls: ['./simple.component.scss']
})
export class SimpleComponent implements OnInit {
export class SimpleComponent implements OnInit, OnDestroy {
liveData: any;
displayTime: string;
timer: number;
interval: any;
private socket: WebSocket | undefined;
private auctionSocket: WebSocket | undefined;
private timerSocket: WebSocket | undefined;

constructor() {
this.timer = 600;
this.displayTime = "10:00"
this.displayTime = ""
this.connect();
}

ngOnInit(): void {
this.loadDefaultData()
this.startTimer();
}

startTimer() {
this.interval = setInterval(() => {
this.timer--;
this.displayTime = this.convertTime(this.timer);

if (this.timer === 0) {
clearInterval(this.interval);
// Дополнительные действия по достижении 0, например, вызов метода или перенаправление на другую страницу.
}
}, 1000);
}

private connect() {
this.socket = new WebSocket('ws://localhost:10000/auction');
this.socket.onopen = () => {
this.auctionSocket = new WebSocket('ws://localhost:10000/auction');
this.auctionSocket.onopen = () => {
console.log('Connected to server');
};
this.socket.onmessage = (event) => {
this.auctionSocket.onmessage = (event) => {
this.liveData = JSON.parse(event.data);
};
this.socket.onclose = (event) => {
this.auctionSocket.onclose = (event) => {
console.log(`WebSocket disconnected with code ${event.code}`);
setTimeout(() => {
console.log('Attempting to reconnect...');
this.connect();
}, 1000);
};
this.socket.onerror = (error) => {
this.auctionSocket.onerror = (error) => {
this.loadDefaultData()
console.log(`WebSocket error: ${error}`);
};

this.timerSocket = new WebSocket('ws://localhost:10000/auctionTimer');
this.timerSocket.onopen = () => {
console.log('Connected to server');
};
this.timerSocket.onmessage = (event) => {
this.displayTime = JSON.parse(event.data).timer;
};
this.timerSocket.onclose = (event) => {
console.log(`WebSocket disconnected with code ${event.code}`);
setTimeout(() => {
console.log('Attempting to reconnect...');
this.connect();
}, 1000);
};
this.timerSocket.onerror = (error) => {
console.log(`WebSocket error: ${error}`);
};
}

private loadDefaultData() {
this.liveData = {
time: "10:00",
rewardCost: "100",
userName: "deema_k",
rewardCost: "",
userName: "",
}
}

convertTime(seconds: number): string {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
const displayMinutes = this.padNumber(minutes);
const displaySeconds = this.padNumber(remainingSeconds);
return `${displayMinutes}:${displaySeconds}`;
}

padNumber(num: number): string {
return num.toString().padStart(2, '0');
ngOnDestroy(): void {
if (this.auctionSocket) {
this.auctionSocket.close();
}
if (this.timerSocket) {
this.timerSocket.close();
}
}

}

0 comments on commit 454cca1

Please sign in to comment.