如何使用 jQuery 和“长轮询”通过 Indy HTTP 服务器动态更新 HTML 页面?

2024-05-07

我读过这篇文章使用 JavaScript 和 jQuery 的简单长轮询示例 http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery。 “长轮询 - 一种高效的服务器推送技术”段落解释了这一点

长轮询技术结合了最佳情况的传统轮询 具有持久的远程服务器连接。术语“长轮询” 它本身是长期持有的 HTTP 请求的缩写。

如何实现使用长轮询的基于 Indy 的 HTTP 服务器?


这是一个独立的示例项目,使用 Indy 版本 10.5.9 和 Delphi 2009 进行了测试。

当应用程序运行时,导航至http://127.0.0.1:8080/。然后,服务器将提供 HTML 文档(在 OnCommandGet 处理程序中硬编码)。

该文档包含一个 div 元素,它将用作新数据的容器:

<body>
  <div>Server time is: <div class="time"></div></div>'
</body>

然后 JavaScript 代码向资源发送请求/getdata在循环中(函数poll()).

服务器响应一个 HTML 片段,其中包含一个新的<div>包含当前服务器时间的元素。然后 JavaScript 代码替换旧的<div>元素与新。

为了模拟服务器工作,该方法在返回数据之前等待一秒钟。

program IndyLongPollingDemo;

{$APPTYPE CONSOLE}

uses
  IdHTTPServer, IdCustomHTTPServer, IdContext, IdSocketHandle, IdGlobal,
  SysUtils, Classes;

type
  TMyServer = class(TIdHTTPServer)
  public
    procedure InitComponent; override;
    procedure DoCommandGet(AContext: TIdContext;
      ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); override;
  end;

procedure Demo;
var
  Server: TMyServer;
begin
  Server := TMyServer.Create(nil);
  try
    try
      Server.Active := True;
    except
      on E: Exception do
      begin
        WriteLn(E.ClassName + ' ' + E.Message);
      end;
    end;
    WriteLn('Hit any key to terminate.');
    ReadLn;
  finally
    Server.Free;
  end;
end;

procedure TMyServer.InitComponent;
var
  Binding: TIdSocketHandle;
begin
  inherited;

  Bindings.Clear;
  Binding := Bindings.Add;
  Binding.IP := '127.0.0.1';
  Binding.Port := 8080;

  KeepAlive := True;
end;

procedure TMyServer.DoCommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
  AResponseInfo.ContentType := 'text/html';
  AResponseInfo.CharSet := 'UTF-8';

  if ARequestInfo.Document = '/' then
  begin
    AResponseInfo.ContentText :=
      '<html>' + #13#10
      + '<head>' + #13#10
      + '<title>Long Poll Example</title>' + #13#10
      + '  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript" charset="utf-8"> ' +
        #13#10
      + '  </script> ' + #13#10
      + '  <script type="text/javascript" charset="utf-8"> ' + #13#10
      + '  $(document).ready(function(){ ' + #13#10
      + '  (function poll(){' + #13#10
      + '  $.ajax({ url: "getdata", success: function(data){' + #13#10
      + '      $("div.time").replaceWith(data);' + #13#10
      + '  }, dataType: "html", complete: poll, timeout: 30000 });' + #13#10
      + '  })();' + #13#10
      + '  });' + #13#10
      + '  </script>' + #13#10
      + '</head>' + #13#10
      + '<body> ' + #13#10
      + '  <div>Server time is: <div class="time"></div></div>' + #13#10
      + '</body>' + #13#10
      + '</html>' + #13#10;
  end
  else
  begin
    Sleep(1000);
    AResponseInfo.ContentText := '<div class="time">'+DateTimeToStr(Now)+'</div>';
  end;
end;

begin
  Demo;
end.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 jQuery 和“长轮询”通过 Indy HTTP 服务器动态更新 HTML 页面? 的相关文章

随机推荐