TsgcWSPServer_Presence

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

 

Methods

All methods are handled internally by the server in response to client requests.

Properties

You must link this component to a Server or to a Broker if you are using more than one protocol.

 

EncodeBase64: by default is disabled, string values are encoded in base64 to avoid problems with JSON encoding.

 

Acknowledgment: if enabled, every time a server sends a message to client assign an ID to this message and queues in a list. When the client receives a message, if detect it has an ID, it sends an Acknowledgment to the server, which means the client has processed message and server can delete from the queue.

 

 

Methods

Events

There are several events to handle actions like: a new member request to join a channel, a new channel is created by a member, a member unsubscribes from a channel...

 


New Member
// When a new client connects to a server, first sends member data to the server to request a new member. 
// Following events can be called:
 
// OnBeforeNewMember: 
// Server receives a request from the client to join and the server accepts or not this member. 
// Use Accept parameter to allow or not this member. 
// By default all members are accepted.
 
procedure OnBeforeNewMember(aConnection:  TsgcWSConnection; const aMember: TsgcWSPresenceMember; 
  var Accept: Boolean);
begin
  if aMember.Name = 'Spam' then
    Accept := False;
end;
 
// OnNewMember: 
// After a new member is accepted, then this event is called and means this member has join member list. You can use aMember.
// Data property to store objects in memory like database access, session objects...
 
procedure OnNewMember(aConnection: TsgcWSConnection; const aMember: TsgcWSPresenceMember);
begin
 
end;

Subscriptions

// When a client has joined as a member, can subscribe to new channels, if a channel not exists, 
// the following events can be called:
 
// OnBeforeNewChannel: 
// Server receives a subscription request from the client to join this channel but the channel doesn't exist, 
// the server can accept or not to create this channel. Use Accept parameter to allow or not this channel. 
// By default, all channels are accepted.
 
procedure OnBeforeNewChannelBeforeNewChannel(Connection: TsgcWSConnection; const aChannel: TsgcWSPresenceChannel; 
  const aMember: TsgcWSPresenceMember; var Accept: Boolean)
begin
  if aChannel.Name = 'Spam' then
    Accept := False;
end;
 
// OnNewChannel: After a new channel is accepted, then this event is called and means a new channel has been created. 
// Channel properties can be customized in this event.
 
procedure OnNewChannel(Connection: TsgcWSConnection; var aChannel: TsgcWSPresenceChannel);
begin
 
end;
 
// If the channel already exists or has been created, the server can accept or no new subscriptions.
 
// OnBeforeNewChannelMembers: 
// Server receives a subscription request from a client to join this channel, the server can accept or not a member join. 
// Use Accept parameter to allow or not this member. By default, all members are accepted.
 
procedure OnBeforeNewChannelMember(Connection: TsgcWSConnection; const aChannel: TsgcWSPresenceChannel; const aMember: TsgcWSPresenceMember; 
  var Accept: Boolean)
begin
  if aMember.Name = 'John' then
    Accept := True
  else if aMember.Name = 'Spam' then
    Accept := False;
end;
 
// OnNewChannelMember: 
// After a new member is accepted,  then this event is called and means a new member has joined the channel.
// All subscribers to this channel, will be notified about new members.
 
procedure OnNewChannelMember(Connection: TsgcWSConnection; const aChannel: TsgcWSPresenceChannel; 
  const aMember: TsgcWSPresenceMember);
begin
end;
 
UnSubscriptions

// Every time a member unjoin a channel or disconnects, the server is notified by following events:
 
// OnRemoveChannelMember: 
// Server receives a subscription request from a client to join this channel but the channel doesn't exist, 
// the server can accept or not to create this channel. Use Accept parameter to allow or not this channel. 
// By default all channels are accepted.
 
procedure OnRemoveChannelMember(Connection: TsgcWSConnection; const aChannel: TsgcWSPresenceChannel; 
  const aMember: TsgcWSPresenceMember);
begin
  Log('Member: ' + aMember.Name + ' unjoin channel: ' + aChannel.Name);
end;
 
// When a member disconnects, automatically server is notified:
 
// OnRemoveMember: when the client disconnects from protocol, this event is called and server is notified of 
// which never has disconnected.
 
procedure OnRemoveMember(aConnection: TsgcWSConnection; aMember: TsgcWSPresenceMember);
begin
  Log('Member: ' + aMember.Name);  
end;
Errors
// Every time there is an error, these events are called, example: server has denied a member to subscribe 
// to a channel, a member try to subscribe to an already subscribed channel...
 
//OnErrorMemberChannel: this event is called every time there is an error trying to create a new channel, 
// join a new member, subscribe to a channel...
 
procedure OnErrorMemberChannel(Connection: TsgcWSConnection; const aError: TsgcWSPresenceError; 
  const aChannel: TsgcWSPresenceChannel; const aMember: TsgcWSPresenceMember);
begin
  Log('#Error: ' + aError.Text);
end;
 
// When a member disconnects, automatically server is notified:
 
// OnErrorPublishMsg: when a client publish a message and this is denied by the server, this 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;