TsgcWSPClient_Presence

This is Server Presence Protocol Component, you need to drop this component in the form and select a TsgcWebSocketClient Component using Client Property.

 

Properties

 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.

 

 

 

Methods

There are several methods to subscribe to a channel, get a list of members...

 

Connect

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


procedure OnNewMember(aConnection: TsgcWSConnection; const aMember: TsgcWSPresenceMember);
begin
  Log('Connected: ' + aMember.Name);
end;

Subscriptions

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.

 


procedure OnNewChannelMember(Connection: TsgcWSConnection; const aChannel: TsgcWSPresenceChannel; 
  const aMember: TsgcWSPresenceMember);
begin
  Log('Subscribed: ' + aChannel.Name);
end;

if the server denies the access to a member, the OnErrorMemberChannel event is raised.


procedure OnErrorMemberChannel(Connection: TsgcWSConnection; const aError: TsgcWSPresenceError; 
  const aChannel: TsgcWSPresenceChannel; const aMember: TsgcWSPresenceMember)
begin
  Log('Error: ' + aError.Text);
end;

Unsubscriptions

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. 


procedure OnRemoveChannelMember(Connection: TsgcWSConnection; const aChannel: TsgcWSPresenceChannel; 
  const aMember: TsgcWSPresenceMember);
begin
  Log('Unsubscribed: ' + aChannel.Name);
end;

If a client can't unsubscribe from a channel, example: because is not subscribed, the OnErrorMemberChannel event is raised.


procedure OnErrorMemberChannel(Connection: TsgcWSConnection; const aError: TsgcWSPresenceError; 
  const aChannel: TsgcWSPresenceChannel; const aMember: TsgcWSPresenceMember)
begin
  Log('Error: ' + aError.Text);
end;

When a client disconnects from the server, the event OnRemoveEvent is called.


procedure OnRemoveMember(aConnection: TsgcWSConnection; aMember: TsgcWSPresenceMember);
begin
  Log('#RemoveMember: ' + aMember.Name);
end;

Publish

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.


procedure OnPublishMsg(Connection: TsgcWSConnection; const aMsg: TsgcWSPresenceMsg; 
  const aChannel: TsgcWSPresenceChannel; const aMember: TsgcWSPresenceMember);
begin
  Log('#PublishMsg: ' + aMsg.Text + ' ' + aMember.Name);
end;

if a message can't be published, the OnErrorPublishMsg event is raised.


procedure OnErrorPublishMsg(Connection: TsgcWSConnection; const aError: TsgcWSPresenceError; 
  const aMsg: TsgcWSPresenceMsg; const aChannel: TsgcWSPresenceChannel; const aMember: TsgcWSPresenceMember);
begin
  Log('#Error: ' + aError.Text);  
end;

GetMembers

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


procedure OnGetMembers(Connection: TsgcWSConnection; const aMembers: TsgcWSPresenceMemberList; 
  const aChannel: TsgcWSPresenceChannel);
var
  i: Integer;
begin
  for i := 0 to aMembers.Count - 1 do
    Log('#GetMembers: ' + aMembers.Member[i].ID + ' ' + aMembers.Member[i].Name);  
end;

If there is an error because the member is not allowed or is not subscribed to channel, the OnErrorMemberChannel event is raised


procedure OnErrorMemberChannel(Connection: TsgcWSConnection; const aError: TsgcWSPresenceError; 
  const aChannel: TsgcWSPresenceChannel; 
  const aMember: TsgcWSPresenceMember);
begin
  Log('Error: ' + aError.Text);
end;

Invite

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.


procedure OnChannelInvitation(Connection: TsgcWSConnection; const aMember: TsgcWSPresenceMember; 
  const aChannel: TsgcWSPresenceChannel; var Accept: Boolean; var ErrorCode: Integer; var ErrorText: string);
begin
  if aChannel = 'MyChannel' then
    Accept := True
  else
    Accept := False;
end;

The member who sends the invitation, can know if the invitation has been accepted or not using the OnChannelInvitationResponse event.


procedure TForm16.PresenceClientChannelInvitationResponse(Connection: TsgcWSConnection; const aMember: TsgcWSPresenceMember; const aChannel: TsgcWSPresenceChannel; Accept: Boolean; const aError: TsgcWSPresenceError);
begin
  if Accept then
    DoLog('#invitation_accepted: [To] ' + aMember.Name + ' [Channel] ' + aChannel.Name)
  else
    DoLog('#invitation_cancelled: [To] ' + aMember.Name + ' [Channel] ' + aChannel.Name + ' [Error] ' + aError.Text);
end;