Connected Property

Example


Applies to
TRPCBroker component

Declaration

property Connected:  Boolean;

Description
This property connects your application to the server:

It is not necessary for your application to manually establish a connection to the server. This version of the Broker automatically connects and disconnects from the server. When you invoke an RPC, if a connection has not already been established, one will be established for you. However, a user will not be able to run your RPC unless a context has been created with the CreateContext method.

There are other advantages to establishing a connection manually. You can check if a connection is established, and branch accordingly depending on whether a connection was established or not. One good place to do this is in your application form's OnCreate event. For that event, you could include code like the following:

try
RPCBroker1.Connected:=True;
except
On EBrokerError do
begin
ShowMessage('Connection to server could not be established!');
Application.Terminate;
end;
end;

This code sets the TRPCBroker component's Connected property to True to establish a connection. If a Broker exception (i.e., EBrokerError) was raised (in which case the connection wasn't established), it provides a message to the user and calls the Terminate method to exit.

To verify that your application is connected to the server, check the value of the Connected property.

If a connected TRPCBroker component is destroyed (when the application is closed, for example), that component will first disconnect from the server. However, for programming clarity, it is advisable to disconnect your application from the server manually by setting the Connected property to False.

If your application uses more than one Broker component, you should be aware of the component's connect and disconnect behavior.