This is Server Presence Protocol Component, you need to drop this component in the form and select a TsgcWebSocketClient Component using Client Property.
EncodeBase64: by default is disabled, string values are encoded in base64 to avoid problems with JSON encoding.
Presence: member data
Acknowledgment: if enabled, every time a client sends a message to server assign an ID to this message and queues in a list. When the server receives the message, if detect it has an ID, it sends an Acknowledgment to the client, which means the server has processed message and the client can delete from the queue.
Interval: interval in seconds where the client checks if there are messages not processed by server.
Timeout: maximum wait time before the client sends the message again.
There are several methods to subscribe to a channel, get a list of members...
When a client connects, the first event called is OnSession, the server sends a session ID to the client, which identifies this client in the server connection list. After OnSession event is called, automatically client sends a request to the server to join as a member, if successful, the OnNewMember event is raised
void OnNewMember(TsgcWSConnection *aConnection, const TsgcWSPresenceMember *aMember)
{
}
When a client wants subscribe to a channel, use the method "Subscribe" and pass the channel name as argument
Client->Subscribe("MyChannel");
If the client is successfully subscribed, the OnNewChannelMember event is called. All members of this channel will be notified using the same event.
void OnNewChannelMember(TsgcWSConnection *aConnection, const TsgcWSPresenceChannel *aChannel,
const TsgcWSPresenceMember *aMember)
{
Log("Subscribed: " + aChannel->Name);
}
if the server denies the access to a member, the OnErrorMemberChannel event is raised.
void OnErrorMemberChannel(TsgcWSConnection *aConnection, const TsgcWSPresenceError *aError;
const TsgcWSPresenceChannel *aChannel,
const TsgcWSPresenceMember *aMember)
{
Log("Error: " + aError->Text);
}
When a client unsubscribe from a channel, use method "Unsubscribe" and pass channel name as argument
Client->Unsubscribe("MyChannel");
If a client is successfully unsubscribed, the OnRemoveChannelMember event is called. All of the members of this channel will be notified using the same event.
void OnRemoveChannelMember(TsgcWSConnection *aConnection, const TsgcWSPresenceChannel *aChannel,
const TsgcWSPresenceMember *aMember)
{
Log("Unsubscribed: " + aChannel->Name);
}
If a client can't unsubscribe from a channel, example: because is not subscribed, the OnErrorMemberChannel event is raised.
void OnErrorMemberChannel(TsgcWSConnection *aConnection, const TsgcWSPresenceError *aError,
const TsgcWSPresenceChannel *aChannel, const TsgcWSPresenceMember *aMember)
{
Log("Error: " + aError->Text);
}
When a client disconnects from the server, the event OnRemoveEvent is called.
void OnRemoveMember(TsgcWSConnection *aConnection, TsgcWSPresenceMember *aMember)
{
Log("#RemoveMember: " + aMember->Name);
}
When a client wants to send a message to all members or all subscribers of a channel, use the Publish method
Client->Publish("Hello All Members");
Client->Publish("Hello All Members of this channel", "MyChannel");
If a message is successfully published, the OnPublishMsg event is called. All members of this channel will be notified using the same event.
void OnPublishMsg(TsgcWSConnection *aConnection, const TsgcWSPresenceMsg *aMsg,
const TsgcWSPresenceChannel *aChannel, const TsgcWSPresenceMember *aMember)
{
Log("#PublishMsg: " + aMsg->Text + " " + aMember->Name);
}
if a message can't be published, the OnErrorPublishMsg event is raised.
void OnErrorPublishMsg(TsgcWSConnection *aConnection, const TsgcWSPresenceError *aError,
const TsgcWSPresenceMsg *aMsg, const TsgcWSPresenceChannel *aChannel, const TsgcWSPresenceMember *aMember)
{
Log("#Error: " + aError->Text);
}
A client can request to the server a list of all members or all members subscribed to a channel. Use the GetMembers method
Client->GetMembers;
Client->GetMembers("MyChannel");
If a message is successfully processed by the server, the OnGetMembers event is called
void OnGetMembers(TsgcWSConnection *aConnection, const TsgcWSPresenceMemberList *aMembers,
const TsgcWSPresenceChannel *aChannel)
{
for (int i = 0; i Count; i++)
{
Log("#GetMembers: " + aMembers->Member[i]->ID + " " + aMembers->Member[i]->Name);
}
}
If there is an error because the member is not allowed or is not subscribed to channel, the OnErrorMemberChannel event is raised
void OnErrorMemberChannel(TsgcWSConnection *aConnection, const TsgcWSPresenceError: +aError,
const TsgcWSPresenceChannel *aChannel, const TsgcWSPresenceMember *aMember)
{
Log("Error: " + aError->Text);
}
A client can invite to other member to subscribe to a channel.
Client->Invite("MyChannel", "E54541D0F0E5R40F1E00FEEA");
When the other member receives the invitation, the OnChannelInvitation event is called and member can Accept or not the invitation.
void OnChannelInvitation(TsgcWSConnection *aConnection, const TsgcWSPresenceMember *aMember,
const TsgcWSPresenceChannel *aChannel, ref bool Accept, ref int ErrorCode, ref string ErrorText)
{
if (aChannel == "MyChannel")
{
Accept = true
}
else
{
Accept = false;
}
}
The member who sends the invitation, can know if the invitation has been accepted or not using the OnChannelInvitationResponse event.
void __fastcall TForm16::PresenceClientChannelInvitationResponse(TsgcWSConnection* Connection, const TsgcWSPresenceMember* aMember, const TsgcWSPresenceChannel* aChannel, bool Accept, const TsgcWSPresenceError* aError)
{
if (Accept)
DoLog("#invitation_accepted: [To] " + aMember->Name + " [Channel] " + aChannel->Name);
else
DoLog("#invitation_cancelled: [To] " + aMember->Name + " [Channel] " + aChannel->Name + " [Error] " + aError->Text);
}