httpsrvsse.dpr 3.5 KB

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