forked from andremussche/DelphiWebsockets
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
IdServerWebsocketContext.pas
108 lines (93 loc) · 3.6 KB
/
IdServerWebsocketContext.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
unit IdServerWebSocketContext;
interface
{$I wsdefines.pas}
uses
System.Classes, System.StrUtils, IdContext, IdCustomTCPServer,
IdCustomHTTPServer, IdIOHandlerWebSocket, IdServerBaseHandling,
IdServerSocketIOHandling, IdWebSocketTypes, IdIIOHandlerWebSocket;
type
TIdServerWSContext = class;
TWebSocketUpgradeEvent = procedure(const AContext: TIdServerWSContext;
const ARequestInfo: TIdHTTPRequestInfo; var Accept: Boolean) of object;
TWebSocketChannelRequest = procedure(const AContext: TIdServerWSContext;
var aType:TWSDataType; const strmRequest, strmResponse: TMemoryStream) of object;
TWebSocketConnected = procedure(const AContext: TIdServerWSContext) of object;
TWebSocketDisconnected = procedure(const AContext: TIdServerWSContext) of object;
TWebSocketConnectionEvents = record
ConnectedEvent: TWebSocketConnected;
DisconnectedEvent: TWebSocketDisconnected;
end;
TIdServerWSContext = class(TIdServerContext)
private
FWebSocketKey: string;
FWebSocketVersion: Integer;
FPath: string;
FWebSocketProtocol: string;
FResourceName: string;
FOrigin: string;
FQuery: string;
FHost: string;
FWebSocketExtensions: string;
FCookie: string;
FClientIP: string;
// FSocketIOPingSend: Boolean;
FOnWebSocketUpgrade: TWebSocketUpgradeEvent;
FOnCustomChannelExecute: TWebSocketChannelRequest;
FSocketIO: TIdServerSocketIOHandling;
FOnDestroy: TIdContextEvent;
function GetClientIP: string;
public
function IOHandler: IIOHandlerWebSocket;
public
function IsSocketIO: Boolean;
property SocketIO: TIdServerSocketIOHandling read FSocketIO write FSocketIO;
//property SocketIO: TIdServerBaseHandling read FSocketIO write FSocketIO;
property OnDestroy: TIdContextEvent read FOnDestroy write FOnDestroy;
public
destructor Destroy; override;
property ClientIP : string read GetClientIP write FClientIP;
property Path : string read FPath write FPath;
property Query : string read FQuery write FQuery;
property ResourceName: string read FResourceName write FResourceName;
property Host : string read FHost write FHost;
property Origin : string read FOrigin write FOrigin;
property Cookie : string read FCookie write FCookie;
property WebSocketKey : string read FWebSocketKey write FWebSocketKey;
property WebSocketProtocol : string read FWebSocketProtocol write FWebSocketProtocol;
property WebSocketVersion : Integer read FWebSocketVersion write FWebSocketVersion;
property WebSocketExtensions: string read FWebSocketExtensions write FWebSocketExtensions;
public
property OnWebSocketUpgrade: TWebSocketUpgradeEvent read FOnWebSocketUpgrade write FOnWebSocketUpgrade;
property OnCustomChannelExecute: TWebSocketChannelRequest read FOnCustomChannelExecute write FOnCustomChannelExecute;
end;
implementation
uses
IdIOHandlerWebSocketSSL, IdIOHandlerStack;
{ TIdServerWSContext }
destructor TIdServerWSContext.Destroy;
begin
if Assigned(OnDestroy) then
OnDestroy(Self);
inherited;
end;
function TIdServerWSContext.GetClientIP: string;
var
LHandler: TIdIOHandlerStack;
begin
if FClientIP = '' then
begin
LHandler := IOHandler as TIdIOHandlerStack;
FClientIP := LHandler.Binding.IP;
end;
Result := FClientIP;
end;
function TIdServerWSContext.IOHandler: IIOHandlerWebSocket;
begin
Result := Connection.IOHandler as IIOHandlerWebSocket;
end;
function TIdServerWSContext.IsSocketIO: Boolean;
begin
//FDocument = '/socket.io/1/websocket/13412152'
Result := StartsText('/socket.io/1/websocket/', FPath);
end;
end.