Skip to content

Commit

Permalink
- custom data property per context (for custom user storage per conne…
Browse files Browse the repository at this point in the history
…ction)

- string events (no superobject parsing but straight sending data)
- enumerate connections
  • Loading branch information
andremussche committed Feb 4, 2014
1 parent 4fc311b commit 49087c6
Showing 1 changed file with 71 additions and 7 deletions.
78 changes: 71 additions & 7 deletions IdSocketIOHandling.pas
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@ EIdSocketIoUnhandledMessage = class(EIdSilentException);

ISocketIOContext = interface
['{ACCAC678-054C-4D75-8BAD-5922F55623AB}']
function GetCustomData: TObject;
function GetOwnsCustomData: Boolean;
procedure SetCustomData(const Value: TObject);
procedure SetOwnsCustomData(const Value: Boolean);

property CustomData: TObject read GetCustomData write SetCustomData;
property OwnsCustomData: Boolean read GetOwnsCustomData write SetOwnsCustomData;

function ResourceName: string;
function PeerIP: string;
function PeerPort: Integer;

procedure EmitEvent(const aEventName: string; const aData: ISuperObject; const aCallback: TSocketIOMsgJSON = nil; const aOnError: TSocketIOError = nil);
procedure EmitEvent(const aEventName: string; const aData: ISuperObject; const aCallback: TSocketIOMsgJSON = nil; const aOnError: TSocketIOError = nil);overload;
procedure EmitEvent(const aEventName: string; const aData: string; const aCallback: TSocketIOMsgJSON = nil; const aOnError: TSocketIOError = nil);overload;
procedure Send(const aData: string; const aCallback: TSocketIOMsgJSON = nil; const aOnError: TSocketIOError = nil);
procedure SendJSON(const aJSON: ISuperObject; const aCallback: TSocketIOMsgJSON = nil; const aOnError: TSocketIOError = nil);
end;
Expand All @@ -45,9 +54,15 @@ TSocketIOContext = class(TInterfacedObject,
FConnectSend: Boolean;
FGUID: string;
FPeerIP: string;
FCustomData: TObject;
FOwnsCustomData: Boolean;
procedure SetContext(const Value: TIdContext);
procedure SetConnectSend(const Value: Boolean);
procedure SetPingSend(const Value: Boolean);
function GetCustomData: TObject;
function GetOwnsCustomData: Boolean;
procedure SetCustomData(const Value: TObject);
procedure SetOwnsCustomData(const Value: Boolean);
protected
FHandling: TIdBaseSocketIOHandling;
FContext: TIdContext;
Expand All @@ -70,18 +85,21 @@ TSocketIOContext = class(TInterfacedObject,
function ResourceName: string;
function PeerIP: string;
function PeerPort: Integer;
function IsDisconnected: Boolean;

property GUID: string read FGUID;
property Context: TIdContext read FContext write SetContext;
property PingSend: Boolean read FPingSend write SetPingSend;
property ConnectSend: Boolean read FConnectSend write SetConnectSend;

function IsDisconnected: Boolean;
property CustomData: TObject read GetCustomData write SetCustomData;
property OwnsCustomData: Boolean read GetOwnsCustomData write SetOwnsCustomData;

//todo: OnEvent per socket
//todo: store session info per connection (see Socket.IO Set + Get -> Storing data associated to a client)
//todo: namespace using "Of"
procedure EmitEvent(const aEventName: string; const aData: ISuperObject; const aCallback: TSocketIOMsgJSON = nil; const aOnError: TSocketIOError = nil);
procedure EmitEvent(const aEventName: string; const aData: ISuperObject; const aCallback: TSocketIOMsgJSON = nil; const aOnError: TSocketIOError = nil);overload;
procedure EmitEvent(const aEventName: string; const aData: string; const aCallback: TSocketIOMsgJSON = nil; const aOnError: TSocketIOError = nil);overload;
// procedure BroadcastEventToOthers(const aEventName: string; const aData: ISuperObject; const aCallback: TSocketIOMsgJSON = nil);
procedure Send(const aData: string; const aCallback: TSocketIOMsgJSON = nil; const aOnError: TSocketIOError = nil);
procedure SendJSON(const aJSON: ISuperObject; const aCallback: TSocketIOMsgJSON = nil; const aOnError: TSocketIOError = nil);
Expand Down Expand Up @@ -163,12 +181,15 @@ TIdBaseSocketIOHandling = class(TIdServerBaseHandling)
procedure OnEvent (const aEventName: string; const aCallback: TSocketIOEvent);
procedure OnConnection(const aCallback: TSocketIONotify);
procedure OnDisconnect(const aCallback: TSocketIONotify);

procedure EnumerateSockets(const aEachSocketCallback: TSocketIONotify);
end;

TIdSocketIOHandling = class(TIdBaseSocketIOHandling)
public
procedure Send(const aMessage: string; const aCallback: TSocketIOMsgJSON = nil; const aOnError: TSocketIOError = nil);
procedure Emit(const aEventName: string; const aData: ISuperObject; const aCallback: TSocketIOMsgJSON = nil; const aOnError: TSocketIOError = nil);
procedure Emit(const aEventName: string; const aData: ISuperObject; const aCallback: TSocketIOMsgJSON = nil; const aOnError: TSocketIOError = nil);overload;
//procedure Emit(const aEventName: string; const aData: string; const aCallback: TSocketIOMsgJSON = nil; const aOnError: TSocketIOError = nil);overload;
end;

implementation
Expand Down Expand Up @@ -251,6 +272,22 @@ destructor TIdBaseSocketIOHandling.Destroy;
inherited;
end;

procedure TIdBaseSocketIOHandling.EnumerateSockets(
const aEachSocketCallback: TSocketIONotify);
var socket: TSocketIOContext;
begin
Assert(Assigned(aEachSocketCallback));
Lock;
try
for socket in FConnections.Values do
aEachSocketCallback(socket);
for socket in FConnectionsGUID.Values do
aEachSocketCallback(socket);
finally
Unlock;
end;
end;

procedure TIdBaseSocketIOHandling.FreeConnection(
const ASocket: TSocketIOContext);
var squid: string;
Expand Down Expand Up @@ -1056,24 +1093,41 @@ destructor TSocketIOContext.Destroy;
FreeAndNil(FQueue);
UnLock;
FLock.Free;
FCustomData.Free;
inherited;
end;

procedure TSocketIOContext.EmitEvent(const aEventName: string; const aData: ISuperObject;
procedure TSocketIOContext.EmitEvent(const aEventName, aData: string;
const aCallback: TSocketIOMsgJSON; const aOnError: TSocketIOError);
begin
if not Assigned(aCallback) then
FHandling.WriteSocketIOEvent(Self, '', aEventName, '[' + aData.AsJSon + ']', nil, nil)
FHandling.WriteSocketIOEvent(Self, '', aEventName, '[' + aData + ']', nil, nil)
else
begin
FHandling.WriteSocketIOEventRef(Self, '', aEventName, '[' + aData.AsJSon + ']',
FHandling.WriteSocketIOEventRef(Self, '', aEventName, '[' + aData + ']',
procedure(const aData: string)
begin
aCallback(Self, SO(aData), nil);
end, aOnError);
end;
end;

procedure TSocketIOContext.EmitEvent(const aEventName: string; const aData: ISuperObject;
const aCallback: TSocketIOMsgJSON; const aOnError: TSocketIOError);
begin
EmitEvent(aEventName, aData.AsJSon, aCallback, aOnError);
end;

function TSocketIOContext.GetCustomData: TObject;
begin
Result := FCustomData;
end;

function TSocketIOContext.GetOwnsCustomData: Boolean;
begin
Result := FOwnsCustomData;
end;

function TSocketIOContext.IsDisconnected: Boolean;
begin
Result := (FClient = nil) and (FContext = nil) and (FGUID = '');
Expand Down Expand Up @@ -1182,6 +1236,16 @@ procedure TSocketIOContext.SetContext(const Value: TIdContext);
(FContext as TIdServerWSContext).OnDestroy := Self.ServerContextDestroy;
end;

procedure TSocketIOContext.SetCustomData(const Value: TObject);
begin
FCustomData := Value;
end;

procedure TSocketIOContext.SetOwnsCustomData(const Value: Boolean);
begin
FOwnsCustomData := Value;
end;

procedure TSocketIOContext.SetPingSend(const Value: Boolean);
begin
FPingSend := Value;
Expand Down

0 comments on commit 49087c6

Please sign in to comment.