HTTPServerSSE_frMain.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2020 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. unit HTTPServerSSE_frMain;
  26. {$MODE DELPHI}
  27. {$PUSH}{$WARN 5024 OFF}
  28. interface
  29. uses
  30. SysUtils,
  31. Classes,
  32. StdCtrls,
  33. ActnList,
  34. Graphics,
  35. Spin,
  36. Dialogs,
  37. Forms,
  38. LCLIntf,
  39. BrookHTTPRequest,
  40. BrookHTTPResponse,
  41. BrookHTTPServer;
  42. type
  43. { TSSEStream }
  44. TSSEStream = class(TStream)
  45. private
  46. FCount: Cardinal;
  47. public
  48. function Read(var ABuffer; ACount: LongInt): LongInt; override;
  49. end;
  50. { TfrMain }
  51. TfrMain = class(TForm)
  52. acStart: TAction;
  53. acStop: TAction;
  54. alMain: TActionList;
  55. BrookHTTPServer1: TBrookHTTPServer;
  56. btStart: TButton;
  57. btStop: TButton;
  58. edPort: TSpinEdit;
  59. lbLink: TLabel;
  60. lbPort: TLabel;
  61. procedure acStartExecute(Sender: TObject);
  62. procedure acStopExecute(Sender: TObject);
  63. procedure BrookHTTPServer1Error(ASender: TObject; AException: Exception);
  64. procedure BrookHTTPServer1Request(ASender: TObject;
  65. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  66. procedure BrookHTTPServer1RequestError(ASender: TObject;
  67. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  68. AException: Exception);
  69. procedure BrookHTTPServer1Start(Sender: TObject);
  70. procedure BrookHTTPServer1Stop(Sender: TObject);
  71. procedure edPortChange(Sender: TObject);
  72. procedure lbLinkClick(Sender: TObject);
  73. procedure lbLinkMouseEnter(Sender: TObject);
  74. procedure lbLinkMouseLeave(Sender: TObject);
  75. protected
  76. procedure DoError(AData: PtrInt);
  77. public
  78. procedure UpdateControls; inline;
  79. end;
  80. const
  81. PAGE_COUNTING = Concat(
  82. '<!DOCTYPE html>', sLineBreak,
  83. '<html>', sLineBreak,
  84. '<head>', sLineBreak,
  85. '<title>SSE example</title>', sLineBreak,
  86. '</head><body><h2 id="counter">Please wait ...</h2>', sLineBreak,
  87. '<script>', sLineBreak,
  88. 'const es = new EventSource("/");', sLineBreak,
  89. 'es.onmessage = function (ev) {', sLineBreak,
  90. ' document.getElementById("counter").innerText = "Counting: " + ev.data;', sLineBreak,
  91. '};', sLineBreak,
  92. '</script>', sLineBreak,
  93. '</body>', sLineBreak,
  94. '</html>'
  95. );
  96. PAGE_ERROR = Concat(
  97. '<!DOCTYPE html>', sLineBreak,
  98. '<html>', sLineBreak,
  99. '<head>', sLineBreak,
  100. '<title>Error</title>', sLineBreak,
  101. '</head>', sLineBreak,
  102. '<body>', sLineBreak,
  103. '<font color="red">%s</font>', sLineBreak,
  104. '</body>', sLineBreak,
  105. '</html>'
  106. );
  107. HTML_HEADER = 'text/html; charset=utf-8';
  108. SSE_HEADER = 'text/event-stream';
  109. IGNORED_ERROR = 'Connection was closed while sending response body.';
  110. var
  111. frMain: TfrMain;
  112. implementation
  113. {$R *.lfm}
  114. { TSSEStream }
  115. function TSSEStream.Read(var ABuffer; ACount: LongInt): LongInt;
  116. var
  117. VMsg: string;
  118. begin
  119. if FCount = 0 then
  120. VMsg := Concat('retry: 1000', sLineBreak)
  121. else
  122. begin
  123. VMsg := Concat('data: ', FCount.ToString, sLineBreak, sLineBreak);
  124. Sleep(1000);
  125. end;
  126. Inc(FCount);
  127. Result := Length(VMsg);
  128. Move(VMsg[1], ABuffer, Result);
  129. end;
  130. { TfrMain }
  131. procedure TfrMain.DoError(AData: PtrInt);
  132. var
  133. S: PString absolute AData;
  134. begin
  135. try
  136. MessageDlg(S^, mtError, [mbOK], 0);
  137. finally
  138. DisposeStr(S);
  139. end;
  140. end;
  141. procedure TfrMain.UpdateControls;
  142. begin
  143. if BrookHTTPServer1.Active then
  144. edPort.Value := BrookHTTPServer1.Port
  145. else
  146. BrookHTTPServer1.Port := edPort.Value;
  147. lbLink.Caption := Concat('http://localhost:', edPort.Value.ToString);
  148. acStart.Enabled := not BrookHTTPServer1.Active;
  149. acStop.Enabled := not acStart.Enabled;
  150. edPort.Enabled := acStart.Enabled;
  151. lbLink.Enabled := not acStart.Enabled;
  152. end;
  153. procedure TfrMain.acStartExecute(Sender: TObject);
  154. begin
  155. BrookHTTPServer1.Open;
  156. end;
  157. procedure TfrMain.acStopExecute(Sender: TObject);
  158. begin
  159. BrookHTTPServer1.Close;
  160. end;
  161. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  162. begin
  163. lbLink.Font.Style := lbLink.Font.Style + [fsUnderline];
  164. end;
  165. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  166. begin
  167. lbLink.Font.Style := lbLink.Font.Style - [fsUnderline];
  168. end;
  169. procedure TfrMain.lbLinkClick(Sender: TObject);
  170. begin
  171. OpenURL(lbLink.Caption);
  172. end;
  173. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  174. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  175. begin
  176. if ARequest.Headers['Accept'] = SSE_HEADER then
  177. begin
  178. AResponse.Headers.Add('Access-Control-Allow-Origin', '*');
  179. AResponse.Headers.Add('Content-Type', SSE_HEADER);
  180. AResponse.SendStream(TSSEStream.Create, 200);
  181. end
  182. else
  183. AResponse.Send(PAGE_COUNTING, HTML_HEADER, 200);
  184. end;
  185. procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
  186. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  187. AException: Exception);
  188. begin
  189. AResponse.SendFmt(PAGE_ERROR, [AException.Message], HTML_HEADER, 500);
  190. end;
  191. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  192. begin
  193. UpdateControls;
  194. end;
  195. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  196. begin
  197. UpdateControls;
  198. end;
  199. procedure TfrMain.edPortChange(Sender: TObject);
  200. begin
  201. UpdateControls;
  202. end;
  203. {$PUSH}{$WARN 4055 OFF}
  204. procedure TfrMain.BrookHTTPServer1Error(ASender: TObject;
  205. AException: Exception);
  206. begin
  207. if AException.Message.TrimRight <> IGNORED_ERROR then
  208. Application.QueueAsyncCall(DoError, PtrInt(NewStr(AException.Message)));
  209. end;
  210. {$POP}
  211. {$POP}
  212. end.