messagehost.lpr 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. {
  2. This file is part of the Free Component Library
  3. Webassembly MessageChannel API - demo host program
  4. Copyright (c) 2025 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program httphost;
  12. {$mode objfpc}
  13. {$modeswitch externalclass}
  14. uses
  15. BrowserConsole, JS, Classes, SysUtils, WebOrWorker, Web, WasiEnv, WasiHostApp,
  16. wasm.pas2js.messagechannelapi;
  17. Type
  18. THostConfig = class external name 'Object' (TJSObject)
  19. wasmFilename : String;
  20. end;
  21. var
  22. HostConfig : THostConfig; external name 'hostConfig';
  23. Type
  24. { TMessageHostApplication }
  25. TMessageHostApplication = class(TBrowserWASIHostApplication)
  26. Private
  27. edtMsg : TJSHTMLInputElement;
  28. btnSend : TJSHTMLButtonElement;
  29. btnSend2 : TJSHTMLButtonElement;
  30. FChannelAPI : TMessageChannelAPI;
  31. FChannel : TJSBroadcastChannel;
  32. function DoHandleMessage(aEvent: TJSEvent): boolean;
  33. function DoSendMessage(aEvent: TJSEvent): boolean;
  34. function DoWasmSendMessage(aEvent: TJSEvent): boolean;
  35. Public
  36. constructor Create(aOwner : TComponent); override;
  37. procedure DoRun; override;
  38. end;
  39. function TMessageHostApplication.DoSendMessage(aEvent: TJSEvent): boolean;
  40. begin
  41. FChannel.postMessage(edtMsg.value);
  42. end;
  43. function TMessageHostApplication.DoWasmSendMessage(aEvent: TJSEvent): boolean;
  44. type
  45. TProcedure = procedure;
  46. var
  47. proc : TProcedure;
  48. begin
  49. Proc:=TProcedure(Exported['SendMessage']);
  50. if assigned(Proc) then
  51. proc;
  52. end;
  53. function TMessageHostApplication.DoHandleMessage(aEvent: TJSEvent): boolean;
  54. var
  55. lMsg : TJSMessageEvent absolute aEvent;
  56. begin
  57. Writeln(lMsg.Data);
  58. end;
  59. constructor TMessageHostApplication.Create(aOwner: TComponent);
  60. begin
  61. inherited Create(aOwner);
  62. FChannel:=TJSBroadcastChannel.new('some_channel');
  63. FChannel.AddEventListener('message',@DoHandleMessage);
  64. FChannelAPI:=TMessageChannelAPI.Create(WasiEnvironment);
  65. RunEntryFunction:='_initialize';
  66. edtMsg:=TJSHTMLInputElement(GetHTMLElement('edtMsg'));
  67. btnSend:=TJSHTMLButtonElement(GetHTMLElement('btnSend'));
  68. btnSend.AddEventListener('click',@DoSendMessage);
  69. btnSend2:=TJSHTMLButtonElement(GetHTMLElement('btnSend2'));
  70. btnSend2.AddEventListener('click',@DoWasmSendMessage);
  71. end;
  72. procedure TMessageHostApplication.DoRun;
  73. var
  74. wasm : String;
  75. begin
  76. Terminate;
  77. if (HostConfig=undefined) and Assigned(HostConfig) and isString(HostConfig.wasmFilename) then
  78. Wasm:=HostConfig.wasmFilename
  79. else
  80. begin
  81. Wasm:=ParamStr(1);
  82. if Wasm='' then
  83. Wasm:='channeldemo.wasm';
  84. end;
  85. StartWebAssembly(Wasm, true);
  86. end;
  87. var
  88. Application : TMessageHostApplication;
  89. begin
  90. Application:=TMessageHostApplication.Create(nil);
  91. Application.Initialize;
  92. Application.Run;
  93. end.