Embarcadero RAD Studio XE

DataSnap Time Server

在這個練習中,Client端定時地向Server端要求系統時間。

Server端

unit ServerMethodsUnit1;

interface

uses System.SysUtils, System.Classes, Datasnap.DSServer, Datasnap.DSAuth;

type
{$METHODINFO ON}
  TServerMethods1 = class(TComponent)
  private
    { Private declarations }
  public
    { Public declarations }
    function getSystemTime : TDateTime;
  end;
{$METHODINFO OFF}

implementation


uses System.StrUtils;

function TServerMethods1.getSystemTime: TDateTime;

begin
  Result := Now;
end;

end.

Client端

unit ClientMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls;

type
  TForm2 = class(TForm)
    Label1: TLabel;
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

uses ClientModuleUnit2;

procedure TForm2.Timer1Timer(Sender: TObject);
var
  sysNow : TDateTime;
begin
  sysNow := ClientModule2.ServerMethods1Client.getSystemTime;  //That's it!
  Label1.Caption := DateToStr(sysNow) + '  ' + TimeToStr(sysNow);
end;

end.