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

Commit

Permalink
Merge pull request #1 from monde-sistemas/improvements
Browse files Browse the repository at this point in the history
Improvements on Error Handling and Connect() usability
  • Loading branch information
RobertoSchneiders committed Sep 25, 2015
2 parents 3632310 + 81467ed commit e1e58cd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ All of them are shipped with this lib releases.

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.

Register the DLL using RegAsm:
```
%windir%\Microsoft.NET\Framework\v4.0.30319\regasm.exe /codebase PusherClientNative.dll /tlb:PusherClientNative.tlb
```

Add `PusherClient` to your unit uses clause.

```
Expand Down Expand Up @@ -56,14 +61,14 @@ end;

SSL is enabled by default. You can disable it by passing a empty option list `[]` to the `Connect` method:
```
PusherClient.Connect('your_pusher_key', []);
PusherClient.Connect('your_pusher_key', '', []);
```

### Custom Host Address

It is possible to use a custom host address:
```
PusherClient.Connect('your_pusher_key', [], 'localhost');
PusherClient.Connect('your_pusher_key', 'localhost');
```
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.

Expand Down
50 changes: 33 additions & 17 deletions delphi/PusherClient/PusherClient.pas
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ TPusherClient = class
procedure Log(Message: string);
procedure ConnectionStateChange(Message: string);
public
class function GetInstance(): TPusherClient;
procedure Connect(Key: string; Options: TConnectionOptions = [coUseSSL];
CustomHost: string = '');
class function Instance(): TPusherClient;
procedure Connect(Key: string; CustomHost: string = ''; Options: TConnectionOptions = [coUseSSL]);
procedure Disconnect();
procedure Subscribe(Channel, EventName: String; Callback: TCallbackProcedure);
property OnError: TCallbackProcedure read FOnError write FOnError;
Expand All @@ -47,20 +46,20 @@ implementation

procedure OnLogStdCall(Message: pchar); stdcall;
begin
TPusherClient.GetInstance.Log(StrPas(Message));
TPusherClient.Instance.Log(StrPas(Message));
end;

procedure OnErrorStdCall(message: pchar); stdcall;
begin
TPusherClient.GetInstance.Error(StrPas(Message));
TPusherClient.Instance.Error(StrPas(Message));
end;

procedure OnSubscribeEventStdCall(Channel: pchar; EventName: pchar; Message: pchar); stdcall;
var
ErrorMessage: string;
begin
try
TPusherClient.GetInstance.FSubscribed[StrPas(Channel)][StrPas(EventName)](StrPas(Message));
TPusherClient.Instance.FSubscribed[StrPas(Channel)][StrPas(EventName)](StrPas(Message));
except
on E:Exception do
begin
Expand All @@ -69,18 +68,18 @@ procedure OnSubscribeEventStdCall(Channel: pchar; EventName: pchar; Message: pch
+ sLineBreak + '[Channel][Event]: Message: [%s][%s]: [%s]'
+ sLineBreak + 'Error: [%s]',
[StrPas(Channel), StrPas(EventName), StrPas(Message), E.Message]);
TPusherClient.GetInstance.Error(ErrorMessage);
TPusherClient.GetInstance.Log(ErrorMessage);
TPusherClient.Instance.Error(ErrorMessage);
TPusherClient.Instance.Log(ErrorMessage);
end;
end;
end;

procedure OnConnectionStateChangeStdCall(message: pchar); stdcall;
begin
TPusherClient.GetInstance.ConnectionStateChange(StrPas(Message));
TPusherClient.Instance.ConnectionStateChange(StrPas(Message));
end;

procedure TPusherClient.Connect(Key: string; Options: TConnectionOptions; CustomHost: string);
procedure TPusherClient.Connect(Key, CustomHost: string; Options: TConnectionOptions);
begin
PusherClientNative.InitializePusherClient(Key, coUseSSL in Options, CustomHost);

Expand Down Expand Up @@ -111,7 +110,7 @@ procedure TPusherClient.Disconnect;
PusherClientNative.Disconnect;
end;

class function TPusherClient.GetInstance: TPusherClient;
class function TPusherClient.Instance: TPusherClient;
begin
if not Assigned(Self.FInstance) then
self.FInstance := TPusherClient.Create;
Expand All @@ -120,20 +119,37 @@ class function TPusherClient.GetInstance: TPusherClient;

procedure TPusherClient.ConnectionStateChange(Message: string);
begin
if Assigned(TPusherClient.GetInstance.FOnConnectionStateChange) then
TPusherClient.GetInstance.FOnConnectionStateChange(Message);
try
if Assigned(FOnConnectionStateChange) then
FOnConnectionStateChange(Message);
except
on E:Exception do
Error('An error occurred while calling the event OnConnectionStateChange: ' + e.message)
end;
end;

procedure TPusherClient.Error(Message: string);
begin
if Assigned(TPusherClient.GetInstance.FOnError) then
TPusherClient.GetInstance.FOnError(Message);
try
if Assigned(FOnError) then
FOnError(Message);
except
// This method cannot fail under any circumstances. It is called by the ComObj callback.
// if some of the others callbacks (OnLog, OnConnectionStateChange) fails they will call this
// method to try to inform the client application about the problem, but, if this method
// fails, there are nothing we can do.
end;
end;

procedure TPusherClient.Log(Message: string);
begin
if Assigned(TPusherClient.GetInstance.FOnLog) then
TPusherClient.GetInstance.FOnLog(Message);
try
if Assigned(FOnLog) then
FOnLog(Message);
except
on E:Exception do
Error('An error occurred while calling the event OnLog: ' + e.message)
end;
end;

class procedure TPusherClient.ReleaseInstance;
Expand Down
16 changes: 7 additions & 9 deletions delphi/example/example.pas
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ TPusherClientExampleForm = class(TForm)
procedure ConnectionStatus(Status: string);
procedure Connect;
procedure Disconnect;
public
{ Public declarations }
end;

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

PusherClient.Subscribe(EdtChannel.Text, EdtEvent.Text,
procedure (Message: string)
begin
Log('[EVENT-MESSAGE]: ' + Message)
end)
PusherClient.Subscribe(EdtChannel.Text, EdtEvent.Text,
procedure (Message: string)
begin
Log('[EVENT-MESSAGE]: ' + Message)
end)
end;

procedure TPusherClientExampleForm.Connect;
Expand All @@ -88,7 +86,7 @@ procedure TPusherClientExampleForm.Connect;
if ChkUseSSL.Checked then
Options := [coUseSSL];

PusherClient.Connect(EdtKey.Text, Options, EdtHost.Text);
PusherClient.Connect(EdtKey.Text, EdtHost.Text, Options);
end;

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

procedure TPusherClientExampleForm.InitializePusherClient;
begin
PusherClient := TPusherClient.GetInstance;
PusherClient := TPusherClient.Instance;
PusherClient.OnError := procedure(Message: string)
begin
Error('[ERROR]: ' + Message);
Expand Down
1 change: 1 addition & 0 deletions dotnet/PusherClientNative/register_dll.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%windir%\Microsoft.NET\Framework\v4.0.30319\regasm.exe /codebase PusherClientNative.dll /tlb:PusherClientNative.tlb

0 comments on commit e1e58cd

Please sign in to comment.