httpsrvsse.lpr 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2021 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. program httpsrvsse;
  26. {$MODE DELPHI}
  27. uses
  28. SysUtils,
  29. Classes,
  30. BrookHTTPRequest,
  31. BrookHTTPResponse,
  32. BrookHTTPServer;
  33. const
  34. PAGE = Concat(
  35. '<!DOCTYPE html>', sLineBreak,
  36. '<html>', sLineBreak,
  37. '<head>', sLineBreak,
  38. '<title>SSE example</title>', sLineBreak,
  39. '</head><body><h2 id="counter">Please wait ...</h2>', sLineBreak,
  40. '<script>', sLineBreak,
  41. 'const es = new EventSource("/");', sLineBreak,
  42. 'es.onmessage = function (ev) {', sLineBreak,
  43. ' document.getElementById("counter").innerText = "Counting: " + ev.data;', sLineBreak,
  44. '};', sLineBreak,
  45. '</script>', sLineBreak,
  46. '</body>', sLineBreak,
  47. '</html>'
  48. );
  49. SSE_HEADER = 'text/event-stream';
  50. IGNORED_ERROR = 'Connection was closed while sending response body.';
  51. type
  52. { TSSEStream }
  53. TSSEStream = class(TStream)
  54. private
  55. FCount: Cardinal;
  56. public
  57. function Read(var ABuffer; ACount: LongInt): LongInt; override;
  58. end;
  59. { THTTPServer }
  60. THTTPServer = class(TBrookHTTPServer)
  61. protected
  62. procedure DoError(ASender: TObject; AException: Exception); override;
  63. procedure DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
  64. AResponse: TBrookHTTPResponse); override;
  65. end;
  66. { TSSEStream }
  67. function TSSEStream.Read(var ABuffer; ACount: LongInt): LongInt;
  68. var
  69. VMsg: string;
  70. begin
  71. if FCount = 0 then
  72. VMsg := Concat('retry: 1000', sLineBreak)
  73. else
  74. begin
  75. VMsg := Concat('data: ', FCount.ToString, sLineBreak, sLineBreak);
  76. Sleep(1000);
  77. end;
  78. Inc(FCount);
  79. Result := Length(VMsg);
  80. Move(VMsg[1], ABuffer, Result);
  81. end;
  82. { THTTPServer }
  83. procedure THTTPServer.DoError(ASender: TObject; AException: Exception);
  84. begin
  85. if AException.Message.TrimRight <> IGNORED_ERROR then
  86. inherited DoError(ASender, AException);
  87. end;
  88. procedure THTTPServer.DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
  89. AResponse: TBrookHTTPResponse);
  90. begin
  91. if ARequest.Headers['Accept'] = SSE_HEADER then
  92. begin
  93. AResponse.Headers.Add('Access-Control-Allow-Origin', '*');
  94. AResponse.Headers.Add('Content-Type', SSE_HEADER);
  95. AResponse.SendStream(TSSEStream.Create, 200);
  96. end
  97. else
  98. AResponse.Send(PAGE, 'text/html; charset=utf-8', 200);
  99. end;
  100. begin
  101. with THTTPServer.Create(nil) do
  102. try
  103. NoFavicon := True;
  104. Threaded := True;
  105. Open;
  106. if not Active then
  107. Exit;
  108. WriteLn('Server running at http://localhost:', Port);
  109. ReadLn;
  110. finally
  111. Free;
  112. end;
  113. end.