Change HTTP Server Port Dynamically

Sure, you can do that with HEScript. There is a code sample here about how to do it:

I think we have a miscommunication.

What I am talking about is use scripting and allow the en user to change the port. I build a form input and user enters their own port #. Or I will use dropdown and allow user to pick a port.

Hope this clears up with I mean.
 
What you’re describing is absolutely feasible. The script mentioned demonstrates how to start and stop the external server, and you can indeed pass the desired port when starting it. In your case, the process would be as follows:
  • Create a form input or dropdown to allow the user to select or enter the desired port number.
  • When the user submits the form, your code should retrieve the selected port and use it to start the external server via the HEScript script call.
  • Make sure not to configure the server to start automatically when your application launches. Instead, control the start-up manually based on the user’s selection.
 
I have tried so many scripting my head is swimming :)

what is wrong with my code:

Code:
procedure StartMyHTTPServer;
var
  portStr: string;
  portNum: integer;
begin
  portStr := GetGlobalVar('SelectedPort', '8142');
  portNum := StrToIntDef(portStr, 8142);

  StartHTTPServer(portNum);
  ShowMessage('Server started on port ' + IntToStr(portNum));
end;

procedure StopMyHTTPServer;
begin
  StopHTTPServer;
  ShowMessage('Server stopped.');
end;

or this:

Code:
procedure StartMyHTTPServer;
var
  portStr: string;
  portNum: integer;
begin
  // Read the port number from a global variable set by JavaScript
  portStr := GetGlobalVar('SelectedPort', '8142');
  portNum := StrToIntDef(portStr, 8142);

  StartHTTPServer(portNum);
  ShowMessage('Server started on port ' + IntToStr(portNum));
end;

procedure StopMyHTTPServer;
begin
  StopHTTPServer;
  ShowMessage('Server stopped.');
end;
 
This is correct HEScript. If it is not working as expected, the problem is almost certainly somewhere else.
In the PHP Settings → External HTTP Server page, you must:
  • Check “Enable External HTTP Server”.
  • Uncheck “Start and stop the HTTP server automatically”
Please also try:

Code:
procedure StartMyHTTPServer;
var
  portStr: string;
  portNum: integer;
begin
  portStr := GetGlobalVar('SelectedPort', '8142');
  ShowMessage('SelectedPort = "' + portStr + '"');  // debug

  portNum := StrToIntDef(portStr, 8142);
  StartHTTPServer(portNum);
  ShowMessage('Server started on port ' + IntToStr(portNum));
end;
 
Back
Top