RealThinClient web server指南.docx
《RealThinClient web server指南.docx》由会员分享,可在线阅读,更多相关《RealThinClient web server指南.docx(54页珍藏版)》请在冰豆网上搜索。
![RealThinClient web server指南.docx](https://file1.bdocx.com/fileroot1/2023-1/22/cf72a429-461f-4b7d-a566-8da407dbeea8/cf72a429-461f-4b7d-a566-8da407dbeea81.gif)
RealThinClientwebserver指南
RealThinClientwebserver指南
YourfirstWebServer(Lesson1)
We’regoingtobuildaWebServerwithRealThinClientSDKinDelphi.
Basicallywearegoingto:
∙CreateaProject
∙AddtwoRealThinClientSDKcomponents(RtcHTTPServerandRtcDataProvider)
∙Configurethreeevents.
∙CheckthatourWebServerisworking.
We’llbeworkingthecodesectionsintwoformats,oneusingthe with clauseandanotherwithoutit.
Steps.
1.CreateanewVCLFormsproject.
InRADStudio,gotoFile->New
CreateanewVCLFormsProjectinDelphi
AfterthisweshouldhaveanewVLCFormcreated.
NewProjectCreatedwithaVCLFormreadytouse
2.AddaRtcHttpServercomponenttoourForm.
Weaddone RtcHttpServer componentfromourcomponentspalette,itshouldbeinthe RTCServergroup.Weareusing RtcHttpServer ComponentbecausewearegoingtocreateanEXEfile,ifwewouldliketogotheDLLwaywithISAPI,thenwewilluse TRtcISAPIServer.
RtcHttpServerComponentinthecomponentspalette
TakeitanddragittotheForm1area.
RtcHttpServerComponentOnForm1
Now,wehaveourcomponentreadytobeused.Weshouldsettheportinwhichourserverwilllistenforrequests,Normally,allwebserverslistenbydefaultonPort80.SowewillmakeourportlisteninPort80,unlessyouhaveanotherwebserverinstalledinyourmachineandlisteninginPort80,youhavetouseanotherport.
Tosettheportwegotoourcomponentproperties.IfyouhavenotselectedtheRtcHttpServer1component,clickonitandgotothepropertiessection.Ifyoudon’tseethepropertieswindowinyourscreen,pressF11.Inthepropertieswindowwewilllookforapropertycalled ServerPort andsetit’svalueto80.ForthisexampleweareusingjustoneServerlisteningonPort80butwemayseveralServersinthesameapplicationlisteninginseveralports.
ServerPortProperty
3.MaketheRtcHttpServer1componentstartlisteningwhenourFormiscreated.
Nowwehavetoinstructour RtcHttpComponent tostartlisteningforrequestsassoonastheformiscreatedandourapplicationstarted.Todothis,weselectour Form1 anddoubleclickonit,or,selectourForm1,gotopropertieswindow,clickonEventstabandthenclickontheOnCreateevent.Delphiwillcreatethehandlerforthe OnCreate()eventandwillshowitonscreen.
FormCreateEvent
Wemustaddthecodetothe FormCreate event.Thisisbecausewewantour RtcHttpServer tostartlisteningforrequestsassoonastheapplicationstartsitsexecutionsowedon’tneedanyotherkindofcontrolstostartourserver.
Code:
1
2
3
4
procedureTForm1.FormCreate(Sender:
TObject);
begin
RtcHttpServer1.Listen();
end;
Now,the Listen() methodforthe RtcHttpServer componentcanacceptoneparameter:
Restarting,thisbydefaultissetto False.Thisparameterworksincombinationwith RestartOn.RestartOnallowstodefinethreeproperties:
∙ListenError:
SetthisparametertoTRUEifyouwantyourservertorestartwhentheserviceisnotabletostart.
∙ListenLost:
SetthisparametertoTRUEifyouwantyourservertorestartwhentheservicestopslisteningforrequestsonthedesignatedport.
∙Wait:
Defineshowmuchtimetheservershouldwaitinsecondsbeforetryingtorestarttheservice.
4.AddaRtcDataProvidertoourForm.
Nowthatwehaveourserverreadytolistenforrequests,weaddone RtcDataProvider componenttotheform.Takea TRtcDataProvider componentfromtheRTCServercomponentgroupanddragittothe Form1.Weneeda RtcDataProvider componentbecauseatthismomentwehaveaServerthatislisteningforrequests,butitwillnotknowwhattodoifarequestarrives,so,foreverytypeofrequestthatwewouldliketogiveananswerinourserver,wehavetodefinean RtcDataProvider.
RtcDataProviderOnPalette
NowweshouldhavetwocomponentsinourForm.
RtcDataProviderComponentOnForm
5.SetRtcDataProviderServerpropertytoRtcHttpServer1.
WemustdefinetheServerforthe RtcDataProvider1 component,inthiscasetheServerwillbeRtcHttpServer1.Weneedtodothisbecausewehavetotellour RtcDataProvider componentwhatServerwilluseincasewehaveotherServers(RtcHttpServer components)listeningondifferentports.
RtcDataProviderLinkedtotheServer
OncetheServerforwhich RtcDataProvider componentwillprocessrequestsisdefined,wehavetodefinewhichrequeststhiscomponentwillprocess.
7.DefinetheOnCheckRequesteventforourRtcDataProvidercomponent.
Tocheckforarequestwewilldefinethe OnCheckRequest eventforthe RtcDataProvider component.Aswesaidbefore,wecanhavemultipleServercomponentslisteningondifferentportsinourapplications,andeveryServermayhavemanyDataProvidersawaitingforrequests.Sothe OnCheckRequest willbecalledonceforallDataProviderslinkedtotheServerwhichhavereceivedtherequest,untiloneoftheDataProviders accept it.
Todothis,withour RtcDataProvider componentselected,weclickonEventsonthePropertieswindowandthendoubleclickonthe OnCheckRequest event
RtcDataProvOnCheckRequestEvent
Thecodewindowwillshowussomethinglikethis
RtcDataProviderOnCheckRequestEventCode
Thecodeinthissectionwillbelikethis:
Using with
1
2
3
4
5
6
procedureTForm1.RtcDataProvider1CheckRequest(Sender:
TRtcConnection);
begin
withSenderasTRtcDataServerdo
ifUpperCase(Request.FileName)='/TIME'then
Accept;
end;
Withoutusing with
1
2
3
4
5
6
7
procedureTForm1.RtcDataProvider1CheckRequest(Sender:
TRtcConnection);
var
rdsSever:
TRtcDataServerabsoluteSender;
begin
ifUpperCase(rdsServer.Request.FileName)='/TIME'then
rdsServer.Accept;
end;
WeareusingSenderas TRtcDataServer sothatwecancompileourapplicationasanstandaloneEXEorasaDLLfile. TRtcDataServer isthebaseclassfor TRtcHttpServer and TRtcISAPIServer,sotokeepcompatibilitywithoutmakinganychangesifwedecidetogoforanyofthesolutions(EXEorDLL),weuseitthisway.
Request.Filename isthecompletefilenamerequestedbytheclient.The Request propertyoftheRtcDataServer componentcanalsogetotherdataasHostname,ContentLengthandType,Queryparameters,HTTPheaders,etc.
Weareacceptingrequestfor“/TIME”.So,inthiscaseanyofthefollowingwillbeprocessed:
∙http:
//localhost/time
∙http:
//localhost/Time
∙http:
//127.0.0.1/TIME
Andanyofitvariations.RememberthatweareconvertingtouppercasethereceivedFileNamerequest,soitwon’tmatterhowtheclientsendstherequestaslongasitisthephrase“/time”.
8.DefinetheOnDataReceivedeventforourRtcDataProvidercomponent.
Now,wedefinethe OnDataReceived eventforthe RtcDataProvider component.OncetheOnCheckRequest eventhasvalidatedthatthe RtcDataProvider shouldaccepttherequest,theOnDataReceived eventiscalledoncetoaccepteverydatapackageuntilallthecontentisreceived,soherewewilldefinewhattheresponseshouldbeforthereceivedrequest.Todothis,withour RtcDataProvidercomponentselected,weclickonEventsatthePropertieswindowandthendoubleclickonthe OnDataReceived event.
RtcDataProviderOnDataReceivedEvent
Thecodewindowwillshowussomethinglikethis
RtcDataProviderOnDataReceivedEventCode
Thecodeinthissectionwilllooklikethis:
Using with
1
2
3
4
5
6
procedureTForm1.RtcDataProvider1DataReceived(Sender:
TRtcConnection);
begin
withSenderasTRtcDataServerdo
ifRequest.Completethen
Write('CurrentTimeis:
'+TimeToStr(Now));
end;
Withoutusing with
1
2
3
4
5
6
7
procedureTForm1.RtcDataProvider1DataReceived(Sender:
TRtcConnection);
var
rdsServer:
TRtcDataServerabsoluteSender;
begin
ifrdsServer.Request.Completethen
rdsServer.Write('Currenttimeis:
'+TimeToStr(Now));
end;
Thepurposeofthe Request.Complete propertyofthe Sender parameteristocheckifwehavereceivedthecompleterequest,becausethe OnCheckRequest eventcouldbecalledmultipletimes.
Usingthe Read methodonthe Sender parameterwillgiveusthecurrentcontentofthereceiverbuffer,thenclearthereceiverbuffercontent.Andbyusingthe Write method,wecansendourresponsebacktotheClient.
9.Compileandruntheproject.
NowweCompileandRuntheProject.Press F9 andyouwillseethisonthescreeniftherearenoerrorsinthecode
ProjectRunning
10.Openyourwebbrowserandgotohttp:
//localhost/time
IfyouselectedPort80theyoucangotohttp:
//localhost/timetocheckServer’sresponse
BrowserShowingServer´sResponse
However,ifyouhaveaservicealreadyrunninganddeclaredaPortotherthan80youshouldspecifyitinthebrowser’saddressbar,somethinglike http:
//localhost:
81/time where81istheportthatyouassignedtoyourapplication
BrowserWithDifferentPort
WiththisweconcludeourfirstDemofortheRealThinClientSDKComponents.
Filesincludedinthispost:
∙SourceCodeforWebServer
∙PDFFileofthisPost
Thisentrywaspostedin Articles, QuickStart, RTCSDK, WEB(HTML/CSS) on 15/02/2013 by JenaroCenteno.
AboutJenaroCenteno
MynameisJenaroCenteno.I’vebeenworkingintheprogrammingareasince1990,atthattimeinDOSenvironments,afterthatontheWindowsplatform.Afewyearsago,ImeettheDelphiLanguage,andinthatmomentIsaid“WhatthehellIwasdoingwithoutthis?
”.Andthentheadventurestarted. MoreAboutMeHere.
ViewallpostsbyJenaroCenteno →
Postnavigation
← PostingcodeexamplestoRTCForumsServersendingdynamicallygeneratedcontent(Lesson2) →
2thoughtson“YourfirstWebServer(Lesson1)”
1.Pingback:
RealThinClientSDK–SendingDynamicallyGeneratedContent|RealThinClassroom
2.Pingback:
RealThinCli