Call Back Channel

Source: Internet
Author: User
Tags call back

Call Back Channel

Datasnap XE3 new Call back channel function. Technical key points:
(1) Tdbxcallback mechanism;
(2) Tdserver.broadcastmessage method;
(3) Tdsclientcallbackchannemanager1.registercallback method;
One, the way of calling back
1. Server-side callback all (same channel) client
2, client callback (same channel) of other clients
3. Clients can register multiple channels

Second, server-side callback channel settings
1, server-side callback channel registration
When programming, the relevant callback channel characters are determined to facilitate the identification of calls in the code.
Const demochannel= ' Demochannel ';

2, server-side callback to the client
The server side uses the Broadcastmessage method of the Tdserver component to call back all registered clients.
The Tdserver component overloads two Broadcastmessage methods
(1) using two parameters
function broadcastmessage (const channelname:string; const Msg:tjsonvalue; Const argtype:integer= Tdbxcallback.argjason): boolean; overload;
The 1th parameter ChannelName all callback clients in the specified channel;
2nd parameter Meg postback;
Function: Send information via MEG to all clients specified with Channelnamer.

(2) using three parameters
function broadcastmessage (const channelname:string; const callbackid:string; Const Msg:tjsonvalue; Const Argtype:integer=tdbxcallback.argjason): Boolean; overload;
The 1th parameter ChannelName all callback clients in the specified channel;
The 2nd parameter callbackid specifies the identification code of the client;
3rd parameter Meg postback;
Function: via MEG to pass information to the Channelnamer, the specified Callbackid client.

Instance: For example, on a server-side Tmemo1.onchange event:
Procedure Tsmainfrm.mmmessagechange (Sender:tobject);
var vmessage:tjsonstring;
Begin
Vmessage: = Tjsonstring.create (MmMessage.Lines.Text);
ServerContainer1.DSServer1.BroadcastMessage (Demochannel, vmessage);
End

3, server-side registration of the client's callback channel search
(1) Search for the client ID of the registration callback channel, implemented by the Dsserver1.getallchannelclientid method, the code is as follows:

Procedure Tform1.btnlistallclientidsclick (Sender:tobject);
Var
aidlist:tlist<string>;
sid:string;
Begin
Aidlist: = ServerContainer5.DSServer1.GetAllChannelClientId (Demochannel);
Try
For sId in Aidlist do
LBALLCLIENTIDS.ITEMS.ADD (SID);
Finally
Aidlist.free;
End
End

(2) Search the callback ID of the callback channel client, implemented by Dsserver1.getallchannelcallbackid method, the code is as follows:

Procedure Tform1.btnlistallcallbackidsclick (sender:tobject);
var
  Aidlist:tlist<string>
  sid:string;
Begin
  aidlist: = Servercontaine R5. Dsserver1.getallchannelcallbackid (Demochannel);
  Try
    for sId in Aidlist do
      LBALLCALLBACKIDS.ITEMS.ADD (SID);
  finally
    aidlist.free
  end;
End;

Third, the client's callback channel settings
1. Client Registration
The client registers the callback channel, which is implemented by the Tdsclientcallbackchannelmanager1.registercallback method and executes the following statement:
Tdsclientcallbackchannemanager1.registercallback (Combobox1.text, tdemocallback.create);
Now let's take a look at the code that contains the four elements of the login callback channel when the client registers the callback channel with the server side:
(1) Callback channel Id,tdsclientcallbackchannemanager.channelnament property value, the related character should be consistent with the callback channel character registered with the server.
(2) Client Id,tdsclientcallbackchannemanager.managerid property value; Each client should specify a unique identification code.
(3) Callback ID, by the Tdsclientcallbackchannemanager1.registercallback method of the 1th parameter;
(4) callback method, brought in by the Tdsclientcallbackchannemanager1.registercallback 2nd parameter, creates a method of Tdemocallback inheritance, which is used to download the Execute method for callback recognition.


2, client and client callback, using the Tdsadminclient category of Notifycallback method
Xe in DSProxy unit, adding tdsadminclient category, enhanced callback function. Where the Notifycallback method of the Tdsadminclient class is used for callback between clients, a client with a specific client (client ID) or callback identification (callback ID) in the specified callback channel can be called back. The statement reads as follows:

function Notifycallback (channelname:string; clientid:string; callbackid:string; Msg:tjsonvalue; Out respones:tjsonvalue): Boolean;

Parameter implication of the Notifycallback method:
(1) ChannelName, call back channel name;
(2) ClientID, client identification;
(3) Callbackid, call back identification;
(4) MSG, transmission of information;
(5) Respones, is called the client's return information.
Examples are as follows:
Procedure Tfmmainform.btnbroadcasttoclientclick (Sender:tobject);
Var
Lclient:tdsadminclient;
lmessage:tjsonstring;
Lresponse:tjsonvalue;
Lconnection:tdbxconnection;
Begin
Lconnection: = scnncallbackserver.dbxconnection;
Lclient: = Tdsadminclient.create (Lconnection, False);
Try
Lmessage: = Tjsonstring.create (Format (' Call Channel:%s, client ID:%s, callback identification:%s, callback information:%s ',
[Demochannelmanager.channelname, Cbclientids.text, Cbcallbackids.text, Mmchannelcallbacks.text]);
Try
Lclient.notifycallback (Demochannelmanager.channelname, Cbclientids.text, Cbcallbackids.text, LMessage, LResponse);
Try
If Lresponse <> Nil Then
Edtresponse.text: = Format (' Client response:%s ', [lresponse.tostring])
Else
Edtresponse.text: = Format (' Client response:%s ', [' nil ']);
Finally
Lresponse.free;
End
Finally
Lmessage.free;
End
Finally
Lclient.free;
End
End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.