- custom data property per context (for custom user storage per connection)
- string events (no superobject parsing but straight sending data) - enumerate connections
This commit is contained in:
parent
4fc311b4c8
commit
49087c6eaa
1 changed files with 71 additions and 7 deletions
|
@ -28,11 +28,20 @@ type
|
||||||
|
|
||||||
ISocketIOContext = interface
|
ISocketIOContext = interface
|
||||||
['{ACCAC678-054C-4D75-8BAD-5922F55623AB}']
|
['{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 ResourceName: string;
|
||||||
function PeerIP: string;
|
function PeerIP: string;
|
||||||
function PeerPort: Integer;
|
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 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);
|
procedure SendJSON(const aJSON: ISuperObject; const aCallback: TSocketIOMsgJSON = nil; const aOnError: TSocketIOError = nil);
|
||||||
end;
|
end;
|
||||||
|
@ -45,9 +54,15 @@ type
|
||||||
FConnectSend: Boolean;
|
FConnectSend: Boolean;
|
||||||
FGUID: string;
|
FGUID: string;
|
||||||
FPeerIP: string;
|
FPeerIP: string;
|
||||||
|
FCustomData: TObject;
|
||||||
|
FOwnsCustomData: Boolean;
|
||||||
procedure SetContext(const Value: TIdContext);
|
procedure SetContext(const Value: TIdContext);
|
||||||
procedure SetConnectSend(const Value: Boolean);
|
procedure SetConnectSend(const Value: Boolean);
|
||||||
procedure SetPingSend(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
|
protected
|
||||||
FHandling: TIdBaseSocketIOHandling;
|
FHandling: TIdBaseSocketIOHandling;
|
||||||
FContext: TIdContext;
|
FContext: TIdContext;
|
||||||
|
@ -70,18 +85,21 @@ type
|
||||||
function ResourceName: string;
|
function ResourceName: string;
|
||||||
function PeerIP: string;
|
function PeerIP: string;
|
||||||
function PeerPort: Integer;
|
function PeerPort: Integer;
|
||||||
|
function IsDisconnected: Boolean;
|
||||||
|
|
||||||
property GUID: string read FGUID;
|
property GUID: string read FGUID;
|
||||||
property Context: TIdContext read FContext write SetContext;
|
property Context: TIdContext read FContext write SetContext;
|
||||||
property PingSend: Boolean read FPingSend write SetPingSend;
|
property PingSend: Boolean read FPingSend write SetPingSend;
|
||||||
property ConnectSend: Boolean read FConnectSend write SetConnectSend;
|
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: OnEvent per socket
|
||||||
//todo: store session info per connection (see Socket.IO Set + Get -> Storing data associated to a client)
|
//todo: store session info per connection (see Socket.IO Set + Get -> Storing data associated to a client)
|
||||||
//todo: namespace using "Of"
|
//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 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 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);
|
procedure SendJSON(const aJSON: ISuperObject; const aCallback: TSocketIOMsgJSON = nil; const aOnError: TSocketIOError = nil);
|
||||||
|
@ -163,12 +181,15 @@ type
|
||||||
procedure OnEvent (const aEventName: string; const aCallback: TSocketIOEvent);
|
procedure OnEvent (const aEventName: string; const aCallback: TSocketIOEvent);
|
||||||
procedure OnConnection(const aCallback: TSocketIONotify);
|
procedure OnConnection(const aCallback: TSocketIONotify);
|
||||||
procedure OnDisconnect(const aCallback: TSocketIONotify);
|
procedure OnDisconnect(const aCallback: TSocketIONotify);
|
||||||
|
|
||||||
|
procedure EnumerateSockets(const aEachSocketCallback: TSocketIONotify);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
TIdSocketIOHandling = class(TIdBaseSocketIOHandling)
|
TIdSocketIOHandling = class(TIdBaseSocketIOHandling)
|
||||||
public
|
public
|
||||||
procedure Send(const aMessage: string; const aCallback: TSocketIOMsgJSON = nil; const aOnError: TSocketIOError = nil);
|
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;
|
end;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
@ -251,6 +272,22 @@ begin
|
||||||
inherited;
|
inherited;
|
||||||
end;
|
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(
|
procedure TIdBaseSocketIOHandling.FreeConnection(
|
||||||
const ASocket: TSocketIOContext);
|
const ASocket: TSocketIOContext);
|
||||||
var squid: string;
|
var squid: string;
|
||||||
|
@ -1056,17 +1093,18 @@ begin
|
||||||
FreeAndNil(FQueue);
|
FreeAndNil(FQueue);
|
||||||
UnLock;
|
UnLock;
|
||||||
FLock.Free;
|
FLock.Free;
|
||||||
|
FCustomData.Free;
|
||||||
inherited;
|
inherited;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TSocketIOContext.EmitEvent(const aEventName: string; const aData: ISuperObject;
|
procedure TSocketIOContext.EmitEvent(const aEventName, aData: string;
|
||||||
const aCallback: TSocketIOMsgJSON; const aOnError: TSocketIOError);
|
const aCallback: TSocketIOMsgJSON; const aOnError: TSocketIOError);
|
||||||
begin
|
begin
|
||||||
if not Assigned(aCallback) then
|
if not Assigned(aCallback) then
|
||||||
FHandling.WriteSocketIOEvent(Self, '', aEventName, '[' + aData.AsJSon + ']', nil, nil)
|
FHandling.WriteSocketIOEvent(Self, '', aEventName, '[' + aData + ']', nil, nil)
|
||||||
else
|
else
|
||||||
begin
|
begin
|
||||||
FHandling.WriteSocketIOEventRef(Self, '', aEventName, '[' + aData.AsJSon + ']',
|
FHandling.WriteSocketIOEventRef(Self, '', aEventName, '[' + aData + ']',
|
||||||
procedure(const aData: string)
|
procedure(const aData: string)
|
||||||
begin
|
begin
|
||||||
aCallback(Self, SO(aData), nil);
|
aCallback(Self, SO(aData), nil);
|
||||||
|
@ -1074,6 +1112,22 @@ begin
|
||||||
end;
|
end;
|
||||||
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;
|
function TSocketIOContext.IsDisconnected: Boolean;
|
||||||
begin
|
begin
|
||||||
Result := (FClient = nil) and (FContext = nil) and (FGUID = '');
|
Result := (FClient = nil) and (FContext = nil) and (FGUID = '');
|
||||||
|
@ -1182,6 +1236,16 @@ begin
|
||||||
(FContext as TIdServerWSContext).OnDestroy := Self.ServerContextDestroy;
|
(FContext as TIdServerWSContext).OnDestroy := Self.ServerContextDestroy;
|
||||||
end;
|
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);
|
procedure TSocketIOContext.SetPingSend(const Value: Boolean);
|
||||||
begin
|
begin
|
||||||
FPingSend := Value;
|
FPingSend := Value;
|
||||||
|
|
Loading…
Reference in a new issue