Skip to content

Commit 2f1b23c

Browse files
committed
optimize load
1 parent 8208247 commit 2f1b23c

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

webapp/chat-app/.gitignore

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ node_modules
2121
npm-debug.log*
2222
yarn-debug.log*
2323
yarn-error.log*
24-
# Gradle files (this is not a Gradle project)
25-
*.gradle
26-
*.gradle.kts
27-
/gradle/
28-
gradlew
29-
gradlew.bat
24+
# Remove any Gradle-related entries if they exist
3025
# dependencies
3126
/node_modules
3227
/.pnp
@@ -47,7 +42,4 @@ gradlew.bat
4742
# IDE
4843
.idea/
4944
.vscode/
50-
*.iml
51-
# Gradle
52-
.gradle/
53-
build/
45+
*.iml
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
+ # Node
2+
+ node_modules/
3+
+ build/
4+
+ dist/
5+
+ coverage/
6+
+
7+
+ # Gradle
8+
+ .gradle/
9+
+ build/
10+
+
11+
+ # IDE
12+
+ .idea/
13+
+ .vscode/
14+
+ *.iml
15+
+
16+
+ # Misc
17+
+ .DS_Store
18+
+ .env.local
19+
+ .env.development.local
20+
+ .env.test.local
21+
+ .env.production.local
22+
+
23+
+ npm-debug.log*
24+
+ yarn-debug.log*
25+
+ yarn-error.log*

webapp/chat-app/src/services/websocket.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export class WebSocketService {
1313
private errorHandlers: ((error: Error) => void)[] = [];
1414
private isReconnecting = false;
1515
private connectionTimeout: NodeJS.Timeout | null = null;
16+
private connectionStartTime = 0;
17+
private messageBuffer: Message[] = [];
18+
private bufferTimeout: NodeJS.Timeout | null = null;
1619

1720
public getSessionId(): string {
1821
console.debug('[WebSocket] Getting session ID:', this.sessionId);
@@ -192,6 +195,7 @@ export class WebSocketService {
192195
console.log('[WebSocket] Connection established successfully');
193196
this.reconnectAttempts = 0;
194197
this.isReconnecting = false;
198+
this.connectionStartTime = Date.now();
195199
this.connectionHandlers.forEach(handler => handler(true));
196200
if (this.connectionTimeout) {
197201
clearTimeout(this.connectionTimeout);
@@ -200,6 +204,9 @@ export class WebSocketService {
200204
};
201205
this.ws.onmessage = (event) => {
202206
this.debugLog('Message received');
207+
const currentTime = Date.now();
208+
const timeSinceConnection = currentTime - this.connectionStartTime;
209+
const shouldBuffer = timeSinceConnection < 10000; // First 10 seconds
203210
// Find the first two comma positions to extract id and version
204211
const firstComma = event.data.indexOf(',');
205212
const secondComma = event.data.indexOf(',', firstComma + 1);
@@ -241,11 +248,30 @@ export class WebSocketService {
241248
console.log('[WebSocket] Processing HTML message');
242249
}
243250

244-
this.messageHandlers.forEach((handler) => handler(message));
251+
if (shouldBuffer) {
252+
this.messageBuffer.push(message);
253+
if (this.bufferTimeout) {
254+
clearTimeout(this.bufferTimeout);
255+
}
256+
this.bufferTimeout = setTimeout(() => {
257+
const messages = [...this.messageBuffer];
258+
this.messageBuffer = [];
259+
messages.forEach(msg => {
260+
this.messageHandlers.forEach(handler => handler(msg));
261+
});
262+
}, 1000);
263+
} else {
264+
this.messageHandlers.forEach((handler) => handler(message));
265+
}
245266
};
246267

247268
this.ws.onclose = () => {
248269
console.log('[WebSocket] Connection closed, stopping heartbeat');
270+
if (this.bufferTimeout) {
271+
clearTimeout(this.bufferTimeout);
272+
this.bufferTimeout = null;
273+
}
274+
this.messageBuffer = [];
249275
this.stopHeartbeat();
250276
this.connectionHandlers.forEach(handler => handler(false));
251277
if (!this.isReconnecting) {

0 commit comments

Comments
 (0)