sam-coder-cli 1.0.22 → 1.0.23
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 +29 -5
- package/package.json +1 -1
|
@@ -19,12 +19,33 @@ async function isPortInUse(port) {
|
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
async function findAvailablePort(startPort = 8080, maxAttempts = 10) {
|
|
23
|
+
let port = startPort;
|
|
24
|
+
let attempts = 0;
|
|
25
|
+
|
|
26
|
+
while (attempts < maxAttempts) {
|
|
27
|
+
const inUse = await isPortInUse(port);
|
|
28
|
+
if (!inUse) {
|
|
29
|
+
return port;
|
|
30
|
+
}
|
|
31
|
+
port++;
|
|
32
|
+
attempts++;
|
|
33
|
+
}
|
|
34
|
+
throw new Error(`Could not find an available port after ${maxAttempts} attempts`);
|
|
35
|
+
}
|
|
36
|
+
|
|
22
37
|
async function ensureServerRunning(port = 8080) {
|
|
23
|
-
|
|
24
|
-
|
|
38
|
+
try {
|
|
39
|
+
const availablePort = await findAvailablePort(port);
|
|
40
|
+
const isDefaultPort = (availablePort === port);
|
|
41
|
+
|
|
42
|
+
if (!isDefaultPort) {
|
|
43
|
+
console.log(chalk.yellow(`Port ${port} is in use, trying port ${availablePort}...`));
|
|
44
|
+
}
|
|
45
|
+
|
|
25
46
|
console.log('Starting local multiplayer server...');
|
|
26
47
|
// Start server in a detached process
|
|
27
|
-
const serverProcess = spawn('node', [__filename, '--server', '--port',
|
|
48
|
+
const serverProcess = spawn('node', [__filename, '--server', '--port', availablePort.toString()], {
|
|
28
49
|
detached: true,
|
|
29
50
|
stdio: 'ignore',
|
|
30
51
|
windowsHide: true
|
|
@@ -34,9 +55,12 @@ async function ensureServerRunning(port = 8080) {
|
|
|
34
55
|
|
|
35
56
|
// Wait for server to start
|
|
36
57
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
37
|
-
console.log(
|
|
58
|
+
console.log(chalk.green(`Local multiplayer server started on port ${availablePort}`));
|
|
59
|
+
return `ws://localhost:${availablePort}`;
|
|
60
|
+
} catch (error) {
|
|
61
|
+
console.error(chalk.red('Failed to start multiplayer server:'), error.message);
|
|
62
|
+
throw error;
|
|
38
63
|
}
|
|
39
|
-
return `ws://localhost:${port}`;
|
|
40
64
|
}
|
|
41
65
|
|
|
42
66
|
class MultiplayerServer {
|