httpsrvsse.lpr 3.4 KB

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