jquery - 如何使用 jQuery 和 "Long Polling"通过 Indy HTTP 服务器动态更新 HTML 页面?

标签 jquery ajax delphi indy long-polling

我已阅读文章Simple Long Polling Example with JavaScript and jQuery 。 “长轮询 - 一种高效的服务器推送技术”段落解释了

the Long Polling technique combines the best-case traditional polling with persistent remote server connections. The term Long Polling itself is short for long-held HTTP request.

如何实现使用长轮询的基于 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.

关于jquery - 如何使用 jQuery 和 "Long Polling"通过 Indy HTTP 服务器动态更新 HTML 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14292475/

相关文章:

javascript - 通过jquery显示php json编码数组的值时出现未定义错误

javascript - Jquery UI 可拖动 : Align helper to mouse position

c# - Delphi 扩展到 C#

delphi - 如何从列表异步加载图片,首先显示文本

c# - 如何从 C# 用户控件中的 jquery 检索值?

jquery解除绑定(bind)点击

ajax - 使用分页的 Wordpress 延迟加载

javascript - 通过 jQuery 进行 AJAX 调用

javascript - 倒数计时器达到 0 后 session 销毁

delphi - 记录类型中的绝对寻址字段