Skip to content

Commit

Permalink
optimize load
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski committed Nov 29, 2024
1 parent 8208247 commit 2f1b23c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
12 changes: 2 additions & 10 deletions webapp/chat-app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ node_modules
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Gradle files (this is not a Gradle project)
*.gradle
*.gradle.kts
/gradle/
gradlew
gradlew.bat
# Remove any Gradle-related entries if they exist
# dependencies
/node_modules
/.pnp
Expand All @@ -47,7 +42,4 @@ gradlew.bat
# IDE
.idea/
.vscode/
*.iml
# Gradle
.gradle/
build/
*.iml
25 changes: 25 additions & 0 deletions webapp/chat-app/chat-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
+ # Node
+ node_modules/
+ build/
+ dist/
+ coverage/
+
+ # Gradle
+ .gradle/
+ build/
+
+ # IDE
+ .idea/
+ .vscode/
+ *.iml
+
+ # Misc
+ .DS_Store
+ .env.local
+ .env.development.local
+ .env.test.local
+ .env.production.local
+
+ npm-debug.log*
+ yarn-debug.log*
+ yarn-error.log*
28 changes: 27 additions & 1 deletion webapp/chat-app/src/services/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export class WebSocketService {
private errorHandlers: ((error: Error) => void)[] = [];
private isReconnecting = false;
private connectionTimeout: NodeJS.Timeout | null = null;
private connectionStartTime = 0;
private messageBuffer: Message[] = [];
private bufferTimeout: NodeJS.Timeout | null = null;

public getSessionId(): string {
console.debug('[WebSocket] Getting session ID:', this.sessionId);
Expand Down Expand Up @@ -192,6 +195,7 @@ export class WebSocketService {
console.log('[WebSocket] Connection established successfully');
this.reconnectAttempts = 0;
this.isReconnecting = false;
this.connectionStartTime = Date.now();
this.connectionHandlers.forEach(handler => handler(true));
if (this.connectionTimeout) {
clearTimeout(this.connectionTimeout);
Expand All @@ -200,6 +204,9 @@ export class WebSocketService {
};
this.ws.onmessage = (event) => {
this.debugLog('Message received');
const currentTime = Date.now();
const timeSinceConnection = currentTime - this.connectionStartTime;
const shouldBuffer = timeSinceConnection < 10000; // First 10 seconds
// Find the first two comma positions to extract id and version
const firstComma = event.data.indexOf(',');
const secondComma = event.data.indexOf(',', firstComma + 1);
Expand Down Expand Up @@ -241,11 +248,30 @@ export class WebSocketService {
console.log('[WebSocket] Processing HTML message');
}

this.messageHandlers.forEach((handler) => handler(message));
if (shouldBuffer) {
this.messageBuffer.push(message);
if (this.bufferTimeout) {
clearTimeout(this.bufferTimeout);
}
this.bufferTimeout = setTimeout(() => {
const messages = [...this.messageBuffer];
this.messageBuffer = [];
messages.forEach(msg => {
this.messageHandlers.forEach(handler => handler(msg));
});
}, 1000);
} else {
this.messageHandlers.forEach((handler) => handler(message));
}
};

this.ws.onclose = () => {
console.log('[WebSocket] Connection closed, stopping heartbeat');
if (this.bufferTimeout) {
clearTimeout(this.bufferTimeout);
this.bufferTimeout = null;
}
this.messageBuffer = [];
this.stopHeartbeat();
this.connectionHandlers.forEach(handler => handler(false));
if (!this.isReconnecting) {
Expand Down

0 comments on commit 2f1b23c

Please sign in to comment.