从其它平台迁移而来


RTC全称RealThinClient,据说是Delphi做三层的神器之一,虽然听说已久,却始终未好好研究过,而且安装包里带的有示例源码,于是乎,索性拿这些源码开这么个系列,督促下自己。


步骤

  1. 创建一个新工程

  2. RTC Server组件页中找到RtcHttpServer组件放到窗体上

  3. 设置RtcHttpServer1ServerPort属性为80

  4. 在窗体的OnCreate事件里写上代码:

1
RtcHttpServer1.Listen;
  1. RTC Server组件页中找到RtcDataProvider组件放到窗体上

  2. 设置RtcDataProvider1Server属性为RtcHttpServer1

  3. RtcDataProvider1OnCheckRequest事件中写上代码:

1
2
3
with Sender as TRtcDataServer do
  if UpperCase(Request.FileName)='/TIME' then
    Accept;
  1. RtcDataProvider1OnDataReceived事件中写上代码:
1
2
3
with Sender as TRtcDataServer do
  if Request.Complete then
    Write('Current time is: '+TimeToStr(Now));
  1. 编译并运行

  2. 打开浏览器,访问网址http://localhost/time

示例源码

核心源码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
procedure TForm1.RtcDataProvider1CheckRequest(Sender: TRtcConnection);
  begin
  with Sender as TRtcDataServer do
    if Request.FilePath.Equal(0,'TIME') then
      Accept;
  end;

procedure TForm1.RtcDataProvider1DataReceived(Sender: TRtcConnection);
  var
    a:integer;
  begin
  with Sender as TRtcDataServer do
    if Request.Complete then
      begin
      Write('<html><body>');
      Write('Request from '+Sender.PeerAddr+':'+Sender.PeerPort+'<br>');
      Write('Server time: '+TimeToStr(Now)+'<br>');

      Write('<br>URL = '+Request.URL+'<br>');

      Write('<br>URI = '+Request.URI+'<br>');

      Write('<br>FileName = '+Request.FileName+'<br>');

      Write('<br>FilePath.Count = '+IntToStr(Request.FilePath.Count)+'<br>');
      for a:=0 to Request.FilePath.Count-1 do
        Write('FilePath['+IntToStr(a)+'] = "'+Request.FilePath[a]+'"<br>');

      Write('<br>Query.ItemCount = '+IntToStr(Request.Query.ItemCount)+'<br>');
      for a:=0 to Request.Query.ItemCount-1 do
        Write('Query Item '+IntToStr(a)+': '+
              'Name = "'+Request.Query.ItemName[a]+'"; '+
              'Value = "'+Request.Query.ItemValue[a]+'"<br>');

      Write('</body></html>');
      end;
  end;

测试

  • 浏览器访问http://localhost/time
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Request from 127.0.0.1:60623
Server time: 21:12:30

URL = localhost/time

URI = /time

FileName = /time

FilePath.Count = 1
FilePath[0] = "time"

Query.ItemCount = 0
  • 浏览器访问http://localhost/times

返回错误

  • 浏览器访问http://localhost/time/test/abc
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Request from 127.0.0.1:60704
Server time: 21:22:32

URL = localhost/time/test/abc

URI = /time/test/abc

FileName = /time/test/abc

FilePath.Count = 3
FilePath[0] = "time"
FilePath[1] = "test"
FilePath[2] = "abc"

Query.ItemCount = 0
  • 浏览器访问http://localhost/time?test=abc
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
Request from 127.0.0.1:60725
Server time: 21:24:06

URL = localhost/time?test=abc

URI = /time?test=abc

FileName = /time

FilePath.Count = 1
FilePath[0] = "time"

Query.ItemCount = 1
Query Item 0: Name = "test"; Value = "abc"

源码解读

  • Request.FilePath.Equal(0,'TIME') VS UpperCase(Request.FileName)='/TIME'

从响应结果可以看出:

  1. 两者都忽略了大小写

  2. 两者都不能响应/times

  3. 前者可以响应以/time开头的所有请求,并且把请求分解后放在FilePath[]里;后者则只能响应/time

  4. 两者都能响应带参数的/time