Home > Back-end >  Delphi server to the client sends a string?
Delphi server to the client sends a string?

Time:09-26


Don't want to get the client IP it takes two to service clients

CodePudding user response:

Use socket ~

CodePudding user response:

Through XE server and the client callback function, can a few from the server to the client at the same time send a message (characters),

CodePudding user response:

http://bbs.csdn.net/topics/390794253

The first server and the client callback

Since Delphi2010 DataSnap support callback (Call Back) mechanism, in this way, the Call takes longer method, through the callback mechanism, to return to the server process or are dealing with the target object,

A, client implementation steps
In the client statement TClientModule1 unit file uClientModule:
1, declare an inherited from TDBXCallback object (for callback parameter values), and realize the complex load steps,

Type
TInitCallback=class (TDBXCallback)
The function the Execute (const Arg: TJSONValue) : TJSONValue; Override.
end;

TProgCallback=class (TDBXCallback)
The function the Execute (const Arg: TJSONValue) : TJSONValue; Override.
end;

2, the initialization to create the object

Initialization//initializes the create
InitCallback:=TInitCallback. Create;
ProgCallback:=TProgCallback. Create;

3, the realization method of the statement

The function TProgCallback. Execute (const Arg: TJSONValue) : TJSONValue;
The begin
With MainFrm do
The begin
ProgressBar1. Position:=TJSONNumber (Arg) AsInt;
Button5. Enabled:=(TJSONNumber (Arg). AsInt=ProgressBar1. Max);
end;
//refresh screen
Application. ProcessMessages;
Result:=TJSONNull. Create;//returns Null values
end;

The function TInitCallback. Execute (const Arg: TJSONValue) : TJSONValue;
The begin
With MainFrm do
The begin
ProgressBar1. Max:=TJSONNumber (Arg) AsInt;
ProgressBar1. Step:=5;
Edit1. Text:='total length:' + TJSONNumber (Arg). ToString + 'seconds';
Button5. Enabled:=False;
end;
Result:=TJSONNull. Create;
end;

Callback function called 4
Procedure TMainFrm. Button5Click (Sender: TObject);
The begin
ClientModule1. ServerMethods1Client. LongTimeRunFunc (InitCallback, ProgCallback);
end;

Second, the server-side implementation steps
On the server side declared TServerMethods1 unit file uServerMethods:
1, write a back to call mechanism method
In the method of parameter into the client's callback parameters:

The function TServerMethods1. LongTimeRunFunc (InitCallback, ProgCallback: TDBXCallback) : Boolean;
Var Max: Integer;
I: Integer;
The begin
Max:=the Random (120) + 1;
InitCallback. Execute (TJSONNumber. Create (Max)), Free;//Max value through JSON format, through InitCallback execution after the callback, the release of the object
For I=0 to Max do
The begin
ProgCallback. Execute (TJSONNumber. Create (I)). The Free;//I value through JSON format, through ProgCallback execution after the callback, the release of the object
Sleep (1000);
end;
Result:=True;
end;

2, declare the method

Public
{Public declarations}
The function LongTimeRunFunc (InitCallback, ProgCallback: TDBXCallback) : Boolean;

Three, the client statement server-side input method of object
Method statement by the server (object), need a statement in the client, to make the data transfer between them,
1, implement the server-side programs,
2, in the client's ClientModule1 unit, through the right mouse button click SQLConnection1, select "Generate DataSnap client classses" project,

Four, running mechanism
Client statement TProgCallback is inherited TDBXCallback object, it Execute complex virtual load method, running on the server side, accept a TJSONValue parameters, and returns a result value, TJSONValue TDBXCallback callback mechanism is very interesting: the server side in the callback client method, can will need back ribbon client value or object, in the form of TJSONValue, as a virtual method the parameters of the Execute the client back ribbon, and the client's method, by the callback after the execution, can also be executed as a result, in the form of TJSONValue, back to the server side,

CodePudding user response:

The second callback channel

DataSnap XE3 new callback channel function, technical key points:
(1) TDBXCallback mechanism;
(2) TDServer. BroadcastMessage method;
(3) TDSClientCallbackChanneManager1. RegisterCallback method;
Three, callback
1, the server callback all client (the same channel)
2, the client callback other client (the same channel)
3, the client can register multiple channel

A, the server side of callback channel setting
1, the server callback channel registration
Programming, the relevant callback channel character, decide, facilitates identification code calls,
Const DEMOChannel='DEMOChannel';

2, the server to the client's callback
The server use TDServer components method BroadcastMessage callback all registered client,
TDServer component overloading the two method BroadcastMessage

(1) using two parametersThe function BroadcastMessage (const ChannelName: String; Const Msg: TJSONValue; Const ArgType: Integer=TDBXCallback. ArgJason) : Boolean; The phrase ";
1 all ChannelName specifies the channel back to the client;
Parameter 2 Meg returned;
Function: all information shall be transmitted by Meg to take ChannelNamer specified client,

(2) using three parameters
The function BroadcastMessage (const ChannelName: String; Const CallbackID: String; Const Msg: TJSONValue; Const ArgType: Integer=TDBXCallback. ArgJason) : Boolean; The phrase ";
1 all ChannelName specifies the channel back to the client;
2 CallbackID specifies the client identification number.
Parameter 3 Meg returned;
Function: delivering information to bring ChannelNamer, Meg CallbackID designated by the client,

Example: for example, in server-side TMemo1. OnChange events:
Procedure TSMainFrm. MmMessageChange (Sender: TObject);
Var vMessage: TJSONString;
The begin
VMessage:=TJSONString. Create (mmMessage. Lines. The Text).
ServerContainer1. DSServer1. BroadcastMessage (DEMOChannel vMessage);
end;

3, the server to the client registered callback channel search
(1) search register callback channel client ID, through DSServer1. GetAllChannelClientId method, the code is as follows:

Procedure TForm1. BtnListAllClientIdsClick (Sender: TObject);
Var
AIdList: TList;
SId: String;
The begin
AIdList:=ServerContainer5. DSServer1. GetAllChannelClientId (DEMOChannel);
Try
For the sId in aIdList do
LbAllClientIds. Items. The Add (sId);
The finally
AIdList. Free;
end;
end;

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related