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

mjn*_*mjn 7 delphi ajax jquery indy long-polling

我已经阅读了使用JavaScript和jQuery的文章Simple Long Polling Example."长轮询 - 高效的服务器推送技术"这一段解释了这一点

Long Polling技术将最佳案例传统轮询与持久远程服务器连接相结合.长轮询本身就是长期持有的HTTP请求的缩写.

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

mjn*_*mjn 6

这是一个独立的示例项目,使用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>
Run Code Online (Sandbox Code Playgroud)

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

服务器使用HTML片段进行响应,该片段包含<div>具有当前服务器时间的新元素.然后JavaScript代码<div>用new 替换旧元素.

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

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.
Run Code Online (Sandbox Code Playgroud)

  • 不要让`TMyServer`为其自己继承的`OnCommandGet`事件分配处理程序,而是让它覆盖虚拟的`TIdCustomHTTPServer.DoCommandGet()`方法.另外,在设置`Response.ContentType`之后设置`Response.CharSet`而不是之前.`ContentType`属性setter可能(以及将来可能会)根据分配的值更改`CharSet`,从而失去手动分配. (2认同)