Browse Source

* Add host config file to configure behavior

Michaël Van Canneyt 11 months ago
parent
commit
6717b54b3e

+ 6 - 0
demo/wasienv/websocket/hostconfig.js

@@ -0,0 +1,6 @@
+var
+  hostConfig = {
+    "wasmFilename" : "wasmwebsocketdemo.wasm",
+    "logWebsocketAPI" : true,
+    "logWasiAPI" : false
+  }

+ 1 - 0
demo/wasienv/websocket/index.html

@@ -5,6 +5,7 @@
   <title>Project1</title>
   <title>Project1</title>
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <link href="bulma.min.css" rel="stylesheet">
   <link href="bulma.min.css" rel="stylesheet">
+  <script src="hostconfig.js"></script>
   <script src="wasmwebsocket.js"></script>
   <script src="wasmwebsocket.js"></script>
 </head>
 </head>
 <body>
 <body>

+ 28 - 2
demo/wasienv/websocket/wasmwebsocket.lpr

@@ -16,11 +16,17 @@
 program wasmwebsocket;
 program wasmwebsocket;
 
 
 {$mode objfpc}
 {$mode objfpc}
+{$modeswitch externalclass}
 
 
 uses
 uses
   BrowserConsole, BrowserApp, WASIHostApp, JS, Classes, SysUtils, WebOrWorker, Web, wasm.pas2js.websocketapi;
   BrowserConsole, BrowserApp, WASIHostApp, JS, Classes, SysUtils, WebOrWorker, Web, wasm.pas2js.websocketapi;
 
 
 type
 type
+  THostConfig = class external name 'Object' (TJSObject)
+    wasmFilename : String;
+    logWebsocketAPI : Boolean;
+    logWasiAPI : Boolean;
+  end;
 
 
   { TMyApplication }
   { TMyApplication }
 
 
@@ -41,9 +47,24 @@ type
     constructor Create(aOwner: TComponent); override;
     constructor Create(aOwner: TComponent); override;
   end;
   end;
 
 
+var
+  HostConfig : THostConfig; external name 'hostConfig';
+
 procedure TMyApplication.DoRun;
 procedure TMyApplication.DoRun;
+var
+  wasm : String;
+
 begin
 begin
-  StartWebAssembly('wasmwebsocketdemo.wasm');
+  Terminate;
+  if Assigned(HostConfig) and isString(HostConfig.wasmFilename) then
+    Wasm:=HostConfig.wasmFilename
+  else
+    begin
+    Wasm:=ParamStr(1);
+    if Wasm='' then
+      Wasm:='wasmwebsocketdemo.wasm';
+    end;
+  StartWebAssembly(wasm);
 end;
 end;
 
 
 procedure TMyApplication.HandleLogClick(aEvent : TJSEvent);
 procedure TMyApplication.HandleLogClick(aEvent : TJSEvent);
@@ -92,12 +113,17 @@ constructor TMyApplication.Create(aOwner: TComponent);
 begin
 begin
   inherited Create(aOwner);
   inherited Create(aOwner);
   FWS:=TWasmWebSocketAPI.Create(WasiEnvironment);
   FWS:=TWasmWebSocketAPI.Create(WasiEnvironment);
-  FWS.LogAPICalls:=True;
+  if isDefined(hostConfig) and Assigned(hostConfig) then
+     begin
+     WasiEnvironment.LogAPI:=HostConfig.logWasiAPi;
+     FWS.LogAPICalls:=HostConfig.logWebsocketAPI;
+     end;
   edtMessage:=TJSHTMLInputElement(GetHTMLElement('edtMessage'));
   edtMessage:=TJSHTMLInputElement(GetHTMLElement('edtMessage'));
   edtFrom:=TJSHTMLInputElement(GetHTMLElement('edtFrom'));
   edtFrom:=TJSHTMLInputElement(GetHTMLElement('edtFrom'));
   edtTo:=TJSHTMLInputElement(GetHTMLElement('edtTo'));
   edtTo:=TJSHTMLInputElement(GetHTMLElement('edtTo'));
   cbLog:=TJSHTMLInputElement(GetHTMLElement('cbLog'));
   cbLog:=TJSHTMLInputElement(GetHTMLElement('cbLog'));
   cbLog.addEventListener('click',@HandleLogClick);
   cbLog.addEventListener('click',@HandleLogClick);
+  cbLog.Checked:=FWS.LogAPICalls;
   btnSend:=TJSHTMLButtonElement(GetHTMLElement('btnSend'));
   btnSend:=TJSHTMLButtonElement(GetHTMLElement('btnSend'));
   btnSend.addEventListener('click',@HandleSendClick);
   btnSend.addEventListener('click',@HandleSendClick);
 end;
 end;