@@ -28,9 +28,8 @@ TPusherClient = class
28
28
procedure Log (Message: string);
29
29
procedure ConnectionStateChange (Message: string);
30
30
public
31
- class function GetInstance (): TPusherClient;
32
- procedure Connect (Key: string; Options: TConnectionOptions = [coUseSSL];
33
- CustomHost: string = ' ' );
31
+ class function Instance (): TPusherClient;
32
+ procedure Connect (Key: string; CustomHost: string = ' ' ; Options: TConnectionOptions = [coUseSSL]);
34
33
procedure Disconnect ();
35
34
procedure Subscribe (Channel, EventName: String; Callback: TCallbackProcedure);
36
35
property OnError: TCallbackProcedure read FOnError write FOnError;
@@ -47,20 +46,20 @@ implementation
47
46
48
47
procedure OnLogStdCall (Message: pchar); stdcall;
49
48
begin
50
- TPusherClient.GetInstance .Log(StrPas(Message));
49
+ TPusherClient.Instance .Log(StrPas(Message));
51
50
end ;
52
51
53
52
procedure OnErrorStdCall (message: pchar); stdcall;
54
53
begin
55
- TPusherClient.GetInstance .Error(StrPas(Message));
54
+ TPusherClient.Instance .Error(StrPas(Message));
56
55
end ;
57
56
58
57
procedure OnSubscribeEventStdCall (Channel: pchar; EventName: pchar; Message: pchar); stdcall;
59
58
var
60
59
ErrorMessage: string;
61
60
begin
62
61
try
63
- TPusherClient.GetInstance .FSubscribed[StrPas(Channel)][StrPas(EventName)](StrPas(Message));
62
+ TPusherClient.Instance .FSubscribed[StrPas(Channel)][StrPas(EventName)](StrPas(Message));
64
63
except
65
64
on E:Exception do
66
65
begin
@@ -69,18 +68,18 @@ procedure OnSubscribeEventStdCall(Channel: pchar; EventName: pchar; Message: pch
69
68
+ sLineBreak + ' [Channel][Event]: Message: [%s][%s]: [%s]'
70
69
+ sLineBreak + ' Error: [%s]' ,
71
70
[StrPas(Channel), StrPas(EventName), StrPas(Message), E.Message]);
72
- TPusherClient.GetInstance .Error(ErrorMessage);
73
- TPusherClient.GetInstance .Log(ErrorMessage);
71
+ TPusherClient.Instance .Error(ErrorMessage);
72
+ TPusherClient.Instance .Log(ErrorMessage);
74
73
end ;
75
74
end ;
76
75
end ;
77
76
78
77
procedure OnConnectionStateChangeStdCall (message: pchar); stdcall;
79
78
begin
80
- TPusherClient.GetInstance .ConnectionStateChange(StrPas(Message));
79
+ TPusherClient.Instance .ConnectionStateChange(StrPas(Message));
81
80
end ;
82
81
83
- procedure TPusherClient.Connect (Key: string; Options: TConnectionOptions; CustomHost: string );
82
+ procedure TPusherClient.Connect (Key, CustomHost : string; Options: TConnectionOptions);
84
83
begin
85
84
PusherClientNative.InitializePusherClient(Key, coUseSSL in Options, CustomHost);
86
85
@@ -111,7 +110,7 @@ procedure TPusherClient.Disconnect;
111
110
PusherClientNative.Disconnect;
112
111
end ;
113
112
114
- class function TPusherClient.GetInstance : TPusherClient;
113
+ class function TPusherClient.Instance : TPusherClient;
115
114
begin
116
115
if not Assigned(Self.FInstance) then
117
116
self.FInstance := TPusherClient.Create;
@@ -120,20 +119,37 @@ class function TPusherClient.GetInstance: TPusherClient;
120
119
121
120
procedure TPusherClient.ConnectionStateChange (Message: string);
122
121
begin
123
- if Assigned(TPusherClient.GetInstance.FOnConnectionStateChange) then
124
- TPusherClient.GetInstance.FOnConnectionStateChange(Message);
122
+ try
123
+ if Assigned(FOnConnectionStateChange) then
124
+ FOnConnectionStateChange(Message);
125
+ except
126
+ on E:Exception do
127
+ Error(' An error occurred while calling the event OnConnectionStateChange: ' + e.message)
128
+ end ;
125
129
end ;
126
130
127
131
procedure TPusherClient.Error (Message: string);
128
132
begin
129
- if Assigned(TPusherClient.GetInstance.FOnError) then
130
- TPusherClient.GetInstance.FOnError(Message);
133
+ try
134
+ if Assigned(FOnError) then
135
+ FOnError(Message);
136
+ except
137
+ // This method cannot fail under any circumstances. It is called by the ComObj callback.
138
+ // if some of the others callbacks (OnLog, OnConnectionStateChange) fails they will call this
139
+ // method to try to inform the client application about the problem, but, if this method
140
+ // fails, there are nothing we can do.
141
+ end ;
131
142
end ;
132
143
133
144
procedure TPusherClient.Log (Message: string);
134
145
begin
135
- if Assigned(TPusherClient.GetInstance.FOnLog) then
136
- TPusherClient.GetInstance.FOnLog(Message);
146
+ try
147
+ if Assigned(FOnLog) then
148
+ FOnLog(Message);
149
+ except
150
+ on E:Exception do
151
+ Error(' An error occurred while calling the event OnLog: ' + e.message)
152
+ end ;
137
153
end ;
138
154
139
155
class procedure TPusherClient.ReleaseInstance ;
0 commit comments