More on ASTA Messaging
Asta provides a solid and easy to use messaging system to facilitate communication between the server and client. Messages consist of an Integer and either a String, a Stream or an AstaParamList (which can contain any datatypes including DataSets). Both TAstaServerSockets and TastaClientSockets have the following message routines. The TAstaClientSocket sends to the implicit ServerSocket while the TAstaServerSocket must identify which client it is sending to.
procedure SendCodedMessage(MsgID: Integer; Msg: string);
procedure SendCodedStream(MsgID: Integer; MS: TMemoryStream);
procedure SendCodedParamList(MsgId: Integer; Params: TAstaParamList);
function SendGetCodedParamList(Msgid: Integer; Params: TAstaParamList);
This is a blocking call and waits for a result from the server.
procedure SendCodedMessage(ClientSocket: TCustomWinSocket; MsgID: Integer;
Msg: string);
procedure SendCodedParamList(ClientSocket: TCustomWinSocket; MsgID: Integer;
Params:TAstaParamList);
procedure SendCodedStream(ClientSocket: TCustomWinSocket; MsgID: Integer;
MS: TMemoryStream);
There are also three events defined on both the TAstaClientSocket and TAstaServerSocket to allow for messages to be received.
property OnCodedMessage: TAstaClientCodedDataEvent;
property OnCodedStream: TCodedStreamEvent;
property OnCodedParamList: TCodedParamEvent;
SendCodedMessage allows you to send a message to the server, and then make a custom response back to the client. The SendCodedMessage method allows you to roll your own protocol where certain MsgIDs have specific meaning to your application. The ability to create your own protocol in this fashion is a simple yet powerful technique. See the Simple Business Objects Example below.
The simple code below sends a message to the server, but the message is sent with two different MsgIDs. The server will handle the message differently depending on which of the MsgIDs, 1700 or 1750, accompanies the message.
procedure TForm1.Button4Click(Sender: TObject);
begin
AstaClientSocket1.SendCodedMessage(1700, eClientCodedMessage.Text);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
AstaClientSocket1.SendCodedMessage(1750, eClientCodedMessage.Text);
end;
The server responds wtih the following implementation code.
procedure TForm1.AstaServerSocket1CodedMessage(Sender: TObject;
MsgID: Integer; Msg: string);
begin
case MsgID of
1700: AstaServerSocket1.SendCodedMessage(MsgID, UpperCase(Msg));
1750: AstaServerSocket1.SendCodedMessage(MsgID, LowerCase(Msg));
end;
end;
When returned to the client, it is picked up in the AstaClientSocket's OnCodedMessage event. Then SendCodedStream(MsgID: Integer; MS: TMemoryStream) compliments the SendCodedMessage method. It can be used to send streams to the server and then the server can respond with a custom action. The following code demonstrates the usage.
The client sends a message to the server.
procedure TForm1.Button1Click(Sender: TObject);
begin
AstaClientSocket1.SendCodedMessage(2000, '');
end;
The server responds by loading a memo into a stream and sending it to the client.
procedure
TForm1.AstaServerSocket1CodedMessage(Sender: TObject;
MsgID: Integer; Msg: string);
var
MS : TMemoryStream;
begin
case MsgID of
2000: begin
MS := TMemoryStream.Create;
mSelectSend.Lines.SaveToStream(MS);
AstaServerSocket1.SendCodedStream(2000, MS);
MS.Free;
end;
end;
end;
The client receives the stream and displays it in a memo and saves it to a file.
procedure TForm1.AstaClientSocket1CodedStream(Sender: TObject;
MsgID: Integer; MS: TMemoryStream);
begin
case MsgID of
2000: begin
mFileFromServer.Lines.Clear;
mFileFromServer.Lines.LoadFromStream(MS);
mFileFromServer.Lines.SaveToFile('ATGTestFile.Txt');
end;
end;
end;
The TAstaParamList allows any number and any kind of data to be transported easily and efficiently. See the CodedParamList Tutorial for an example of using an AstaParamList to send a TextFile from a client to the server. Not only does it send the file, but the name of the file and the time it was sent. The same tutorial contains an example of sending a zip file from the client to the server, demonstrating how AstaParamLists can be used to transport binary files.
Chat Messaging
Procedure SendChatPopup(S: String) will broadcast a message from the sending client to all the other clients connected to the same server. This message is intrusive and it will stop the other clients from working while the message is displayed in a modal dialog that halts the other clients' progress. See SendChatEvent for an alternative.
The following code reads the input from a Memo named mChatMessage and broadcasts it to all the clients (including the sending client) in a popup dialog box.
procedure TForm1.Button1Click(Sender: TObject);
begin
AstaClientSocket1.SendChatPopup(mChatMessage.Text);
end;
Procedure SendChatEvent(S: String) broadcasts a message from the sending client to all other clients connected to the same server, but it is not intrusive.like SendChatPopup. This message must be intercepted and handled in the OnChatMessage event handler. If you do not assign the event handler, then the message will NOT be displayed.
This code broadcasts a message from the client to all the other clients.
Simple Business Objects Example
The following code shows how you could send CodedMessages to a business object instantiated on the server. In this scenario, 1800 and 1850 are "protocol codes" that you control. The client can call those codes, with or without a parameter and invoke a custom response from the server.
procedure TForm1.AstaServerSocket1CodedMessage(Sender: TObject;
MsgID: Integer; Msg: string);
begin
case MsgID of
1800: begin
if MyBusinessObject.ChargeSalesTax(StrToInt(Msg)) then
AstaServerSocket1.SendCodedMessage(MsgID,'TRUE')
else
AstaServerSocket1.SendCodedMessage(MsgID,'FALSE');
end;
1850: begin
AstaServerSocket1.SendCodedMessage(MsgID,
MyBusinessObject.NewCreditLimit(Msg));
end;
end;
end;