Detect running server port number from terminal output.
This commit is contained in:
parent
6a31161c0a
commit
6c615f1a4f
@ -156,10 +156,6 @@ io.on("connection", async (socket) => {
|
|||||||
if (!containers[data.sandboxId]) {
|
if (!containers[data.sandboxId]) {
|
||||||
containers[data.sandboxId] = await Sandbox.create();
|
containers[data.sandboxId] = await Sandbox.create();
|
||||||
console.log("Created container ", data.sandboxId);
|
console.log("Created container ", data.sandboxId);
|
||||||
io.emit(
|
|
||||||
"previewURL",
|
|
||||||
"https://" + containers[data.sandboxId].getHostname(3000)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(`Error creating container ${data.sandboxId}:`, e);
|
console.error(`Error creating container ${data.sandboxId}:`, e);
|
||||||
@ -492,8 +488,26 @@ io.on("connection", async (socket) => {
|
|||||||
await lockManager.acquireLock(data.sandboxId, async () => {
|
await lockManager.acquireLock(data.sandboxId, async () => {
|
||||||
try {
|
try {
|
||||||
terminals[id] = await containers[data.sandboxId].terminal.start({
|
terminals[id] = await containers[data.sandboxId].terminal.start({
|
||||||
onData: (data: string) => {
|
onData: (responseData: string) => {
|
||||||
io.emit("terminalResponse", { id, data });
|
io.emit("terminalResponse", { id, data: responseData });
|
||||||
|
|
||||||
|
function extractPortNumber(inputString: string) {
|
||||||
|
// Remove ANSI escape codes
|
||||||
|
const cleanedString = inputString.replace(/\x1B\[[0-9;]*m/g, '');
|
||||||
|
// Regular expression to match port number
|
||||||
|
const regex = /http:\/\/localhost:(\d+)/;
|
||||||
|
// If a match is found, return the port number
|
||||||
|
const match = cleanedString.match(regex);
|
||||||
|
return match ? match[1] : null;
|
||||||
|
}
|
||||||
|
const port = parseInt(extractPortNumber(responseData) ?? "");
|
||||||
|
if (port) {
|
||||||
|
io.emit(
|
||||||
|
"previewURL",
|
||||||
|
"https://" + containers[data.sandboxId].getHostname(port)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
size: { cols: 80, rows: 20 },
|
size: { cols: 80, rows: 20 },
|
||||||
onExit: () => console.log("Terminal exited", id),
|
onExit: () => console.log("Terminal exited", id),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user