Skip to content
This repository was archived by the owner on Jan 11, 2022. It is now read-only.

Commit e1e58cd

Browse files
Merge pull request #1 from monde-sistemas/improvements
Improvements on Error Handling and Connect() usability
2 parents 3632310 + 81467ed commit e1e58cd

File tree

4 files changed

+48
-28
lines changed

4 files changed

+48
-28
lines changed

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ All of them are shipped with this lib releases.
2929

3030
Download the [last release](https://github.com/monde-sistemas/pusher-websocket-delphi/releases/latest) zip package and add it to your project. Make sure all the dependencies are on the same folder that your exe.
3131

32+
Register the DLL using RegAsm:
33+
```
34+
%windir%\Microsoft.NET\Framework\v4.0.30319\regasm.exe /codebase PusherClientNative.dll /tlb:PusherClientNative.tlb
35+
```
36+
3237
Add `PusherClient` to your unit uses clause.
3338

3439
```
@@ -56,14 +61,14 @@ end;
5661

5762
SSL is enabled by default. You can disable it by passing a empty option list `[]` to the `Connect` method:
5863
```
59-
PusherClient.Connect('your_pusher_key', []);
64+
PusherClient.Connect('your_pusher_key', '', []);
6065
```
6166

6267
### Custom Host Address
6368

6469
It is possible to use a custom host address:
6570
```
66-
PusherClient.Connect('your_pusher_key', [], 'localhost');
71+
PusherClient.Connect('your_pusher_key', 'localhost');
6772
```
6873
The [default value](https://github.com/pusher-community/pusher-websocket-dotnet/blob/master/PusherClient/Pusher.cs#L43) is `ws.pusherapp.com` which is the pusher.com endpoint, but you can also use it with a [poxa](https://github.com/edgurgel/poxa) server hosted in your own server.
6974

delphi/PusherClient/PusherClient.pas

+33-17
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ TPusherClient = class
2828
procedure Log(Message: string);
2929
procedure ConnectionStateChange(Message: string);
3030
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]);
3433
procedure Disconnect();
3534
procedure Subscribe(Channel, EventName: String; Callback: TCallbackProcedure);
3635
property OnError: TCallbackProcedure read FOnError write FOnError;
@@ -47,20 +46,20 @@ implementation
4746

4847
procedure OnLogStdCall(Message: pchar); stdcall;
4948
begin
50-
TPusherClient.GetInstance.Log(StrPas(Message));
49+
TPusherClient.Instance.Log(StrPas(Message));
5150
end;
5251

5352
procedure OnErrorStdCall(message: pchar); stdcall;
5453
begin
55-
TPusherClient.GetInstance.Error(StrPas(Message));
54+
TPusherClient.Instance.Error(StrPas(Message));
5655
end;
5756

5857
procedure OnSubscribeEventStdCall(Channel: pchar; EventName: pchar; Message: pchar); stdcall;
5958
var
6059
ErrorMessage: string;
6160
begin
6261
try
63-
TPusherClient.GetInstance.FSubscribed[StrPas(Channel)][StrPas(EventName)](StrPas(Message));
62+
TPusherClient.Instance.FSubscribed[StrPas(Channel)][StrPas(EventName)](StrPas(Message));
6463
except
6564
on E:Exception do
6665
begin
@@ -69,18 +68,18 @@ procedure OnSubscribeEventStdCall(Channel: pchar; EventName: pchar; Message: pch
6968
+ sLineBreak + '[Channel][Event]: Message: [%s][%s]: [%s]'
7069
+ sLineBreak + 'Error: [%s]',
7170
[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);
7473
end;
7574
end;
7675
end;
7776

7877
procedure OnConnectionStateChangeStdCall(message: pchar); stdcall;
7978
begin
80-
TPusherClient.GetInstance.ConnectionStateChange(StrPas(Message));
79+
TPusherClient.Instance.ConnectionStateChange(StrPas(Message));
8180
end;
8281

83-
procedure TPusherClient.Connect(Key: string; Options: TConnectionOptions; CustomHost: string);
82+
procedure TPusherClient.Connect(Key, CustomHost: string; Options: TConnectionOptions);
8483
begin
8584
PusherClientNative.InitializePusherClient(Key, coUseSSL in Options, CustomHost);
8685

@@ -111,7 +110,7 @@ procedure TPusherClient.Disconnect;
111110
PusherClientNative.Disconnect;
112111
end;
113112

114-
class function TPusherClient.GetInstance: TPusherClient;
113+
class function TPusherClient.Instance: TPusherClient;
115114
begin
116115
if not Assigned(Self.FInstance) then
117116
self.FInstance := TPusherClient.Create;
@@ -120,20 +119,37 @@ class function TPusherClient.GetInstance: TPusherClient;
120119

121120
procedure TPusherClient.ConnectionStateChange(Message: string);
122121
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;
125129
end;
126130

127131
procedure TPusherClient.Error(Message: string);
128132
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;
131142
end;
132143

133144
procedure TPusherClient.Log(Message: string);
134145
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;
137153
end;
138154

139155
class procedure TPusherClient.ReleaseInstance;

delphi/example/example.pas

+7-9
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ TPusherClientExampleForm = class(TForm)
3333
procedure ConnectionStatus(Status: string);
3434
procedure Connect;
3535
procedure Disconnect;
36-
public
37-
{ Public declarations }
3836
end;
3937

4038
var
@@ -73,11 +71,11 @@ procedure TPusherClientExampleForm.BtnSubscribeClick(Sender: TObject);
7371
if SubscribeList.Items.IndexOf(ListItem) = -1 then
7472
SubscribeList.Items.Add(EdtChannel.Text + ' | ' + EdtEvent.Text);
7573

76-
PusherClient.Subscribe(EdtChannel.Text, EdtEvent.Text,
77-
procedure (Message: string)
78-
begin
79-
Log('[EVENT-MESSAGE]: ' + Message)
80-
end)
74+
PusherClient.Subscribe(EdtChannel.Text, EdtEvent.Text,
75+
procedure (Message: string)
76+
begin
77+
Log('[EVENT-MESSAGE]: ' + Message)
78+
end)
8179
end;
8280

8381
procedure TPusherClientExampleForm.Connect;
@@ -88,7 +86,7 @@ procedure TPusherClientExampleForm.Connect;
8886
if ChkUseSSL.Checked then
8987
Options := [coUseSSL];
9088

91-
PusherClient.Connect(EdtKey.Text, Options, EdtHost.Text);
89+
PusherClient.Connect(EdtKey.Text, EdtHost.Text, Options);
9290
end;
9391

9492
procedure TPusherClientExampleForm.ConnectionStatus(Status: string);
@@ -115,7 +113,7 @@ procedure TPusherClientExampleForm.Error(Message: string);
115113

116114
procedure TPusherClientExampleForm.InitializePusherClient;
117115
begin
118-
PusherClient := TPusherClient.GetInstance;
116+
PusherClient := TPusherClient.Instance;
119117
PusherClient.OnError := procedure(Message: string)
120118
begin
121119
Error('[ERROR]: ' + Message);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
%windir%\Microsoft.NET\Framework\v4.0.30319\regasm.exe /codebase PusherClientNative.dll /tlb:PusherClientNative.tlb

0 commit comments

Comments
 (0)