оптимизон от нейронки, доделал автоматические действия, добавил глобальный лог, сделал реконекты к ДА
This commit is contained in:
@@ -3,11 +3,12 @@ unit uWSDA;
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, System.JSON, ipwwsclient, StrUtils, uAPIDA;
|
||||
Classes, SysUtils, System.JSON, ipwwsclient, StrUtils, uAPIDA;
|
||||
|
||||
type
|
||||
TOnDonateEvent = procedure(aNick, aMessage, aSum: string) of object;
|
||||
TOnStatusEvent = procedure(AStatusText: string; AStatusCode:integer) of object;
|
||||
TOnStatusEvent = procedure(AStatusText: string; AStatusCode: integer) of object;
|
||||
TOnLog = procedure(aModul: string; aMethod: string; aMessage: string; aLevel: integer) of object;
|
||||
|
||||
TWSClient = class(TObject)
|
||||
private
|
||||
@@ -15,16 +16,17 @@ type
|
||||
FAPIClient: TAPIClient;
|
||||
FOnDonate: TOnDonateEvent;
|
||||
FOnStatus: TOnStatusEvent;
|
||||
FOnLog: TOnLog;
|
||||
FWsstoken: string;
|
||||
FWSID: string;
|
||||
procedure DataIn(Sender: TObject; DataFormat: Integer; const Text: string;
|
||||
procedure DataIn(Sender: TObject; DataFormat: integer; const Text: string;
|
||||
const TextB: TBytes; EOM, EOL: Boolean);
|
||||
procedure ConnectionStatus(Sender: TObject; const ConnectionEvent: string;
|
||||
StatusCode: Integer; const Description: string);
|
||||
procedure Error(Sender: TObject; ErrorCode: Integer;
|
||||
const Description: string);
|
||||
StatusCode: integer; const Description: string);
|
||||
procedure Error(Sender: TObject; ErrorCode: integer; const Description: string);
|
||||
function ExtractValue(const T_, Text, _T: string): string;
|
||||
procedure HandleIncomingData(const Data: string);
|
||||
procedure toLog(aLevel: integer; aMethod: string; aMessage: string);
|
||||
public
|
||||
constructor Create;
|
||||
destructor Destroy; override;
|
||||
@@ -33,6 +35,7 @@ type
|
||||
procedure Send(const Data: string);
|
||||
property OnDonate: TOnDonateEvent read FOnDonate write FOnDonate;
|
||||
property OnStatus: TOnStatusEvent read FOnStatus write FOnStatus;
|
||||
property OnLog: TOnLog read FOnLog write FOnLog;
|
||||
property Wsstoken: string read FWsstoken write FWsstoken;
|
||||
property WSID: string read FWSID write FWSID;
|
||||
property APIClient: TAPIClient read FAPIClient write FAPIClient;
|
||||
@@ -42,60 +45,112 @@ implementation
|
||||
|
||||
constructor TWSClient.Create;
|
||||
begin
|
||||
inherited Create;
|
||||
FWS := TipwWSClient.Create(nil);
|
||||
FWS.OnDataIn := DataIn;
|
||||
FWS.OnConnectionStatus := ConnectionStatus;
|
||||
FWS.OnError := Error;
|
||||
|
||||
FAPIClient := nil;
|
||||
FOnDonate := nil;
|
||||
FOnStatus := nil;
|
||||
FOnLog := nil;
|
||||
end;
|
||||
|
||||
destructor TWSClient.Destroy;
|
||||
begin
|
||||
FWS.Disconnect;
|
||||
FWS.Free;
|
||||
try
|
||||
if Assigned(FWS) then
|
||||
begin
|
||||
try
|
||||
// î÷èñòèì îáðàáîò÷èêè ÷òîáû íå áûëî îáðàòíûõ âûçîâîâ â ìîìåíò îñâîáîæäåíèÿ
|
||||
FWS.OnDataIn := nil;
|
||||
FWS.OnConnectionStatus := nil;
|
||||
FWS.OnError := nil;
|
||||
try
|
||||
FWS.Disconnect;
|
||||
except
|
||||
// èãíîðèðóåì îøèáêè ïðè îòêëþ÷åíèè
|
||||
end;
|
||||
finally
|
||||
FreeAndNil(FWS);
|
||||
end;
|
||||
end;
|
||||
except
|
||||
// íè÷åãî íå äåëàåì — çàùèòà îò èñêëþ÷åíèé â äåñòðóêòîðå
|
||||
end;
|
||||
inherited;
|
||||
end;
|
||||
|
||||
procedure TWSClient.Disconnect;
|
||||
begin
|
||||
FWS.Disconnect;
|
||||
if Assigned(FWS) then
|
||||
begin
|
||||
try
|
||||
FWS.Disconnect;
|
||||
except
|
||||
// èãíîðèðóåì
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TWSClient.Connect(const WSSURL: string);
|
||||
begin
|
||||
FWS.ConnectTo(WSSURL);
|
||||
if Assigned(FWS) then
|
||||
begin
|
||||
try
|
||||
FWS.ConnectTo(WSSURL);
|
||||
except
|
||||
// ëîãèðîâàòü ïðè íåîáõîäèìîñòè
|
||||
toLog(2, 'Connect', 'Exception on Connect');
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TWSClient.Send(const Data: string);
|
||||
begin
|
||||
FWS.SendText(Data);
|
||||
if Assigned(FWS) then
|
||||
begin
|
||||
try
|
||||
FWS.SendText(Data);
|
||||
except
|
||||
toLog(2, 'Send', 'Exception on Send');
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TWSClient.DataIn(Sender: TObject; DataFormat: Integer;
|
||||
procedure TWSClient.toLog(aLevel: integer; aMethod: string; aMessage: string);
|
||||
begin
|
||||
if Assigned(FOnLog) then
|
||||
FOnLog('uWSDA', aMethod, aMessage, aLevel);
|
||||
end;
|
||||
|
||||
procedure TWSClient.DataIn(Sender: TObject; DataFormat: integer;
|
||||
const Text: string; const TextB: TBytes; EOM, EOL: Boolean);
|
||||
begin
|
||||
HandleIncomingData(Text);
|
||||
// FWS.Ping;
|
||||
try
|
||||
HandleIncomingData(Text);
|
||||
except
|
||||
on E: Exception do
|
||||
toLog(2, 'DataIn', E.Message);
|
||||
end;
|
||||
//FWS.Ping; // åñëè íóæíî
|
||||
end;
|
||||
|
||||
procedure TWSClient.ConnectionStatus(Sender: TObject;
|
||||
const ConnectionEvent: string; StatusCode: Integer;
|
||||
const Description: string);
|
||||
procedure TWSClient.ConnectionStatus(Sender: TObject; const ConnectionEvent: string;
|
||||
StatusCode: integer; const Description: string);
|
||||
begin
|
||||
if Assigned(FOnStatus) then
|
||||
FOnStatus(ConnectionEvent,StatusCode);
|
||||
FOnStatus(ConnectionEvent, StatusCode);
|
||||
end;
|
||||
|
||||
procedure TWSClient.Error(Sender: TObject; ErrorCode: Integer;
|
||||
const Description: string);
|
||||
procedure TWSClient.Error(Sender: TObject; ErrorCode: integer; const Description: string);
|
||||
begin
|
||||
// fLog.toLog(2, 'uWSDA', 'Error', 'Code: ' + IntToStr(ErrorCode) + ' - ' +
|
||||
// Description);
|
||||
toLog(2, 'Error', '[' + IntToStr(ErrorCode) + '] ' + Description);
|
||||
end;
|
||||
|
||||
function TWSClient.ExtractValue(const T_, Text, _T: string): string;
|
||||
var
|
||||
StartPos, EndPos: Integer;
|
||||
StartPos, EndPos: integer;
|
||||
begin
|
||||
StartPos := Pos(T_, Text);
|
||||
if StartPos = 0 then
|
||||
@@ -110,42 +165,59 @@ end;
|
||||
procedure TWSClient.HandleIncomingData(const Data: string);
|
||||
var
|
||||
JSON: TJSONObject;
|
||||
jo: TJSONObject;
|
||||
DataObj: TJSONObject;
|
||||
DonationData: TJSONObject;
|
||||
ChannelArray: TJSONArray;
|
||||
jo: TJSONObject;
|
||||
wsstoken2: string;
|
||||
begin
|
||||
// fLog.toLog(3, 'uWSDA', 'HandleIncomingData', Data);
|
||||
toLog(3, 'HandleIncomingData', Data);
|
||||
// Îáðàáîòêà ðåãèñòðàöèè êëèåíòà
|
||||
if Pos('"result":{"client":"', Data) > 0 then
|
||||
begin
|
||||
FWsstoken := ExtractValue('"result":{"client":"', Data, '",');
|
||||
// fLog.toLog(3, 'uWSDA', 'HandleIncomingData', 'Êëèåíò çàðåãèñòðèðîâàí');
|
||||
jo := FAPIClient.SubscribeToChannel(FWSID, FWsstoken);
|
||||
// fLog.toLog(3, 'uWSDA', 'HandleIncomingData', 'Êëèåíò ïîäïèñàí');
|
||||
ChannelArray := jo.Values['channels'] as TJSONArray;
|
||||
if Assigned(ChannelArray) and (ChannelArray.Count > 0) then
|
||||
toLog(3, 'HandleIncomingData', 'Êëèåíò çàðåãèñòðèðîâàí');
|
||||
if Assigned(FAPIClient) then
|
||||
begin
|
||||
wsstoken2 := ChannelArray.Items[0].GetValue<string>('token');
|
||||
// fLog.toLog(3, 'da', 'EventWS', 'Ïîäïèñêà íà êàíàë ñ òîêåíîì: ' +
|
||||
// wsstoken2);
|
||||
FWS.SendText('{"params": {"channel": "$alerts:donation_' + FWSID +
|
||||
'","token": "' + wsstoken2 + '"},"method": 1,"id": 2 }');
|
||||
try
|
||||
jo := FAPIClient.SubscribeToChannel(FWSID, FWsstoken);
|
||||
except
|
||||
jo := nil;
|
||||
end;
|
||||
if Assigned(jo) then
|
||||
try
|
||||
toLog(3, 'HandleIncomingData', 'Êëèåíò ïîäïèñàí');
|
||||
ChannelArray := jo.Values['channels'] as TJSONArray;
|
||||
if Assigned(ChannelArray) and (ChannelArray.Count > 0) then
|
||||
begin
|
||||
wsstoken2 := ChannelArray.Items[0].GetValue<string>('token');
|
||||
toLog(3, 'HandleIncomingData', 'Ïîäïèñêà íà êàíàë ñ òîêåíîì: ' + wsstoken2);
|
||||
try
|
||||
FWS.SendText('{"params": {"channel": "$alerts:donation_' + FWSID + '","token": "' + wsstoken2 + '"},"method": 1,"id": 2 }');
|
||||
except
|
||||
toLog(2, 'HandleIncomingData', 'SendText failed');
|
||||
end;
|
||||
end;
|
||||
finally
|
||||
jo.Free;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
// Îáðàáîòêà äîíàòîâ
|
||||
if Pos('"name":"Donations"', Data) > 0 then
|
||||
begin
|
||||
// fLog.toLog(3, 'uWSDA', 'HandleIncomingData', 'Íîâûé Äîíàò');
|
||||
JSON := TJSONObject.ParseJSONValue(Data) as TJSONObject;
|
||||
toLog(3, 'HandleIncomingData', 'Íîâûé Äîíàò');
|
||||
JSON := nil;
|
||||
try
|
||||
DataObj := JSON.GetValue<TJSONObject>('result').GetValue<TJSONObject>
|
||||
('data').GetValue<TJSONObject>('data');
|
||||
if Assigned(DataObj) and Assigned(FOnDonate) then
|
||||
FOnDonate(DataObj.GetValue<string>('username'),
|
||||
DataObj.GetValue<string>('message'),
|
||||
DataObj.GetValue<string>('amount'));
|
||||
JSON := TJSONObject.ParseJSONValue(Data) as TJSONObject;
|
||||
if Assigned(JSON) then
|
||||
begin
|
||||
DataObj := JSON.GetValue<TJSONObject>('result').GetValue<TJSONObject>('data').GetValue<TJSONObject>('data');
|
||||
if Assigned(DataObj) and Assigned(FOnDonate) then
|
||||
FOnDonate(DataObj.GetValue<string>('username'),
|
||||
DataObj.GetValue<string>('message'),
|
||||
DataObj.GetValue<string>('amount'));
|
||||
end;
|
||||
finally
|
||||
JSON.Free;
|
||||
end;
|
||||
@@ -153,3 +225,4 @@ begin
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user