从其它平台迁移而来


/SQUARE

  1. 打开上节的工程

  2. 添加组件RtcDataProvider2并设置Server属性为RtcHttpServer1

  3. RtcDataProvider2OnCheckRequest事件中写上代码:

1
2
3
with TRtcDataServer(Sender) do
  if UpperCase(Request.FileName)='/SQUARE' then
    Accept;
  1. RtcDataProvider2OnDataReceived事件中写上代码:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
var
  line:integer;
begin
with TRtcDataServer(Sender) do
  if Request.Complete then
    begin
    Write('<html><body>');
    Write('Here comes a table of square values ... <br>');
    for line:=1 to 100 do
      begin
      // 使用3 write 和使用1个效果是一样的
      Write('Square of '+IntToStr(line)+' = ');
      Write(IntToStr(line*line));
      Write('<br>');
      end;
    Write('......... done.');
    Write('</body></html>');
    end;
end;
  1. 编译并运行

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

细节

  • TRtcDataProvider只响应自己接收的请求,互不干涉

  • TRtcDataServer(Sender) VS Sender as TRtcDataServer,考察下as强转的效果和区别

  • 使用多个write()与使用一个效果一样

带参数的/SQUARE

  1. 修改RtcDataProvider2OnDataReceived事件的代码为:
 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
var
  cnt,line:integer;
begin
with TRtcDataServer(Sender) do
  if Request.Complete then
    begin
    Write('<html><body>');
    Write('Here comes a table of square values ... <br>');
    cnt:=0;
    if Request.Query['cnt']<>'' then
      try
        cnt:=StrToInt(Request.Query['cnt']);
      except
      end;
    if (cnt<1)or (cnt>1000) then
      begin
      cnt:=10;
      Write('Wrong "cnt" parameter.');
      Write('Using default value of 10.<br>');
      end;
    for line:=1 to cnt do
      begin
      Write('Square of '+IntToStr(line)+' = '+IntToStr(line*line));
      Write('<br>');
      end;
    Write('......... done.');
    Write('</body></html>');
    end;
end;
  1. 编译并运行

  2. 打开浏览器,分别访问网址http://localhost/squarehttp://localhost/square?cnt=30http://localhost/square?cnt=5392