wasmwebsocket.lpr 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. {
  2. This file is part of the Free Component Library
  3. Webassembly Websocket API demo.
  4. Copyright (c) 2024 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 wasmwebsocket;
  12. {$mode objfpc}
  13. {$modeswitch externalclass}
  14. uses
  15. BrowserConsole, BrowserApp, WASIHostApp, JS, Classes, SysUtils, WebOrWorker, Web, wasm.pas2js.websocketapi;
  16. type
  17. THostConfig = class external name 'Object' (TJSObject)
  18. wasmFilename : String;
  19. logWebsocketAPI : Boolean;
  20. logWasiAPI : Boolean;
  21. end;
  22. { TMyApplication }
  23. TMyApplication = class(TWASIHostApplication)
  24. private
  25. FWS: TWasmWebSocketAPI;
  26. cbLog,
  27. edtFrom,
  28. edtTo,
  29. edtMessage : TJSHTMLInputElement;
  30. btnSend : TJSHTMLButtonElement;
  31. procedure HandleLogClick(aEvent: TJSEvent);
  32. procedure HandleSendClick(aEvent: TJSEvent);
  33. protected
  34. procedure SendMessageToWasm(aMsg : string);
  35. procedure DoRun; override;
  36. public
  37. constructor Create(aOwner: TComponent); override;
  38. end;
  39. var
  40. HostConfig : THostConfig; external name 'hostConfig';
  41. procedure TMyApplication.DoRun;
  42. var
  43. wasm : String;
  44. begin
  45. Terminate;
  46. if Assigned(HostConfig) and isString(HostConfig.wasmFilename) then
  47. Wasm:=HostConfig.wasmFilename
  48. else
  49. begin
  50. Wasm:=ParamStr(1);
  51. if Wasm='' then
  52. Wasm:='wasmwebsocketdemo.wasm';
  53. end;
  54. StartWebAssembly(wasm);
  55. end;
  56. procedure TMyApplication.HandleLogClick(aEvent : TJSEvent);
  57. begin
  58. FWS.LogApiCalls:=cbLog.Checked;
  59. end;
  60. procedure TMyApplication.HandleSendClick(aEvent : TJSEvent);
  61. begin
  62. SendMessageToWasm(edtMessage.Value);
  63. edtMessage.Value:='';
  64. edtTo.Value:='';
  65. end;
  66. procedure TMyApplication.SendMessageToWasm(aMsg: string);
  67. type
  68. TSendProcedure = procedure (Buf : Longint; BufLen : Longint);
  69. var
  70. CB : JSValue;
  71. CallBack : TSendProcedure absolute CB;
  72. Bfr : TJSUint8Array;
  73. Enc : TJSTextEncoder;
  74. Loc,lLen : Longint;
  75. payload : string;
  76. begin
  77. CB:=WasiEnvironment.Instance.exports_['sendmessage'];
  78. if isFunction(CB) then
  79. begin
  80. PayLoad:=TJSJSON.StringIfy(New(['msg',aMsg,'from',edtFrom.value,'recip',edtTo.value]));
  81. Enc:=TJSTextEncoder.new;
  82. Bfr:=Enc.encode(PayLoad);
  83. lLen:=Bfr.byteLength;
  84. Loc:=FWS.InstanceExports.AllocMem(lLen);
  85. WasiEnvironment.SetUTF8StringInMem(Loc,lLen,Bfr);
  86. CallBack(Loc,llen);
  87. FWS.InstanceExports.freeMem(Loc);
  88. end;
  89. end;
  90. constructor TMyApplication.Create(aOwner: TComponent);
  91. begin
  92. inherited Create(aOwner);
  93. FWS:=TWasmWebSocketAPI.Create(WasiEnvironment);
  94. if isDefined(hostConfig) and Assigned(hostConfig) then
  95. begin
  96. WasiEnvironment.LogAPI:=HostConfig.logWasiAPi;
  97. FWS.LogAPICalls:=HostConfig.logWebsocketAPI;
  98. end;
  99. edtMessage:=TJSHTMLInputElement(GetHTMLElement('edtMessage'));
  100. edtFrom:=TJSHTMLInputElement(GetHTMLElement('edtFrom'));
  101. edtTo:=TJSHTMLInputElement(GetHTMLElement('edtTo'));
  102. cbLog:=TJSHTMLInputElement(GetHTMLElement('cbLog'));
  103. cbLog.addEventListener('click',@HandleLogClick);
  104. cbLog.Checked:=FWS.LogAPICalls;
  105. btnSend:=TJSHTMLButtonElement(GetHTMLElement('btnSend'));
  106. btnSend.addEventListener('click',@HandleSendClick);
  107. end;
  108. var
  109. Application : TMyApplication;
  110. begin
  111. Application:=TMyApplication.Create(nil);
  112. Application.Initialize;
  113. Application.Run;
  114. end.