Products

Price/Order

Support

Partners

Testimonials

Test Results

About us

Contact
 Client Lesson 2: Call your first RTC remote function
Bottom
 
Total posts: 1
 Author Client Lesson 2: Call your first RTC remote function
Danijel Tkalcec [RTC]

02.06.2006 01:37:06
Registered user
This is the first client lesson for calling remote functions. You will learn in this lesson how to use rtcClientModule and rtcResult components to call remote functions served by a RTC server. This lesson continues where Server Lesson 5 stops. You can find the complete source code and textual version of this lesson below.

Estimated time to complete: 8 minutes


Click HERE to start the visual lesson


Here is an example how you can write a client to call remote functions from a server.

This example includes 2 source code versions.
The first version will use the "with" statements (default way of writing your code once you are familiar with the components and want to code a more readable code) and one without using the with statements (if you want to learn to which objects the methods/properties belong in this examples).
Both source code versions will be presented in the same code box (like this one), separated by a line like this:

--- NOT using "with" ---

Do not copy-paste both parts, since they are identical. The second version (not using the "with" statement) is included only for learning purposes.


1. Start a new Project in Delphi

2. put one Edit field on your form (name "Edit1")
3. for Edit1, set Text = 'User'
4. put one Memo field on your form (name "Memo1")
5. for Memo1, set ScrollBars = ssBoth
6. from the "RTC Client" tab, put one RtcHttpClient on your main form:
7. for RtcHttpClient1, set AutoConnect = True
8. for RtcHttpClient1, set MultiThreaded = True
9. for RtcHttpClient1, set ReconnectOn.ConnectLost = True
10. for RtcHttpClient1, set ServerAddr = localhost
11. for RtcHttpClient1, set ServerPort = 80
12. for RtcHttpClient1, set UseProxy = True
13. from the "RTC Client" tab, put one RtcClientModule on your form:  
14. for RtcClientModule1, set Client = RtcHttpClient1
15. for RtcClientModule1, set AutoRepost = 2
16. for RtcClientModule1, set AutoSyncEvents = True
17. for RtcClientModule1, set ModuleFileName = '/mytest'
18. from the "RTC Client" tab, put one RtcResult on your form:
19. for RtcResult1, define the "OnReturn" event:
 Memo1.Lines.Add( Result.asString );

20. for Edit1, define the "OnKeyPress" event:
 if Key=#13 then
    begin
    Edit1.SelectAll;
    with RtcClientModule1 do
      begin
      // Prepare a new remote function call
      with Data.newFunction('hello') do
        begin
        // set function call parameters
        asString['name']:=Edit1.Text;
        end;
      // Call the remote function
      Call(RtcResult1);
      end;
    end;

--- NOT using "with" ---

  if Key=#13 then
    begin
    Edit1.SelectAll;
    // Prepare a new remote function call
    RtcClientModule1.Data.newFunction('hello');
    // set function call parameters
    RtcClientModule1.Data.asFunction.asString['name']:=Edit1.Text;
    // Call the remote function
    RtcClientModule1.Call(RtcResult1);
    end;

--- NOT using "with", version 2 ---

  if Key=#13 then
    begin
    Edit1.SelectAll;
    // Prepare a new remote function call and set function parameter
    RtcClientModule1.Data.newFunction('hello').asString['name']:=Edit1.Text;
    // Call the remote function
    RtcClientModule1.Call(RtcResult1);
    end;

21. Start the Server (from Server Lesson 5), then Compile and Run this client project.

If everything went ok, after you start both projects, when you enter some text in the Edit field and press the Enter key, you will receive a response in your Memo component.

In this example, we are passing a single string parameter ('name') and receive a single string as a result. You can pass any kind of data to the server as parameters and receive any structure as the result. Please check the FAQ for more info.

NOTE: To issue more than 1 function call in a single HTTP request, you can use "RtcClientModule1.StartCalls;" before you start preparing your function calls and "RtcClientModule1.Post;" after the last function call has been issued (using the "Call" method). But, you can not use "Data.newFunction" more than once before a single "Call". An exception will be raised that object was already assigned, in case you try it. You will get the same exception rregularly if you try using the same rtcClientModule to send requests from multiple threads, without setting "rtcClientModule1.TyperThreading" property to True, to ensure a separate Data property will be used for each thread.