sam-coder-cli 1.0.23 → 1.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/multiplayer-client.js +68 -39
- package/package.json +1 -1
|
@@ -272,49 +272,78 @@ class MultiplayerClient extends EventEmitter {
|
|
|
272
272
|
}
|
|
273
273
|
|
|
274
274
|
async connect() {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
this.serverUrl = await ensureServerRunning(this.port);
|
|
278
|
-
|
|
279
|
-
this.ws = new WebSocket(this.serverUrl);
|
|
280
|
-
|
|
281
|
-
this.ws.on('open', () => {
|
|
282
|
-
this.connected = true;
|
|
283
|
-
this.emit('connected');
|
|
284
|
-
console.log(chalk.green('✅ Connected to multiplayer server'));
|
|
285
|
-
|
|
286
|
-
// Send client info immediately after connection
|
|
287
|
-
this.updateClientInfo({
|
|
288
|
-
name: this.name,
|
|
289
|
-
role: this.role,
|
|
290
|
-
model: this.model
|
|
291
|
-
});
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
this.ws.on('message', (data) => {
|
|
275
|
+
return new Promise((resolve, reject) => {
|
|
276
|
+
const connectWithRetry = async () => {
|
|
295
277
|
try {
|
|
296
|
-
|
|
297
|
-
this.
|
|
278
|
+
// If no server URL is provided, try to start a local server
|
|
279
|
+
if (this.serverUrl === 'ws://localhost:8080') {
|
|
280
|
+
try {
|
|
281
|
+
this.serverUrl = await ensureServerRunning(8080);
|
|
282
|
+
} catch (error) {
|
|
283
|
+
console.error(chalk.red('Failed to start local server:'), error.message);
|
|
284
|
+
reject(error);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
console.log(chalk.blue(`Connecting to ${this.serverUrl}...`));
|
|
290
|
+
this.ws = new WebSocket(this.serverUrl);
|
|
291
|
+
|
|
292
|
+
const connectionTimeout = setTimeout(() => {
|
|
293
|
+
this.ws.terminate();
|
|
294
|
+
reject(new Error('Connection timeout'));
|
|
295
|
+
}, 10000); // 10 seconds timeout
|
|
296
|
+
|
|
297
|
+
this.ws.on('open', () => {
|
|
298
|
+
clearTimeout(connectionTimeout);
|
|
299
|
+
this.connected = true;
|
|
300
|
+
console.log(chalk.green('✅ Connected to multiplayer server'));
|
|
301
|
+
|
|
302
|
+
// Send client info
|
|
303
|
+
this.send({
|
|
304
|
+
type: 'client_info',
|
|
305
|
+
clientId: this.clientId,
|
|
306
|
+
name: this.name,
|
|
307
|
+
role: this.role,
|
|
308
|
+
model: this.model
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
this.emit('connected');
|
|
312
|
+
resolve();
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
this.ws.on('message', (data) => {
|
|
316
|
+
try {
|
|
317
|
+
const message = JSON.parse(data);
|
|
318
|
+
this.handleMessage(message);
|
|
319
|
+
} catch (error) {
|
|
320
|
+
console.error('Error parsing message:', error);
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
this.ws.on('close', () => {
|
|
325
|
+
this.connected = false;
|
|
326
|
+
this.emit('disconnected');
|
|
327
|
+
console.log(chalk.yellow('\n🔌 Disconnected from multiplayer server'));
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
this.ws.on('error', (error) => {
|
|
331
|
+
clearTimeout(connectionTimeout);
|
|
332
|
+
console.error('WebSocket error:', error.message);
|
|
333
|
+
if (!this.connected) {
|
|
334
|
+
reject(error);
|
|
335
|
+
}
|
|
336
|
+
this.emit('error', error);
|
|
337
|
+
});
|
|
338
|
+
|
|
298
339
|
} catch (error) {
|
|
299
|
-
console.error('
|
|
340
|
+
console.error('Connection error:', error.message);
|
|
341
|
+
reject(error);
|
|
300
342
|
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
this.ws.on('close', () => {
|
|
304
|
-
this.connected = false;
|
|
305
|
-
this.emit('disconnected');
|
|
306
|
-
console.log(chalk.yellow('\n🔌 Disconnected from multiplayer server'));
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
this.ws.on('error', (error) => {
|
|
310
|
-
console.error('WebSocket error:', error);
|
|
311
|
-
this.emit('error', error);
|
|
312
|
-
});
|
|
343
|
+
};
|
|
313
344
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
this.emit('error', error);
|
|
317
|
-
}
|
|
345
|
+
connectWithRetry();
|
|
346
|
+
});
|
|
318
347
|
}
|
|
319
348
|
|
|
320
349
|
handleMessage(message) {
|