добавил часть модулей, нужно переделать БД на records
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
unit uWSDA;
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, System.JSON, ipwwsclient, StrUtils, uAPIDA;
|
||||
|
||||
type
|
||||
TOnDonateEvent = procedure(aNick, aMessage, aSum: string) of object;
|
||||
TOnStatusEvent = procedure(AStatusText: string; AStatusCode:integer) of object;
|
||||
|
||||
TWSClient = class(TObject)
|
||||
private
|
||||
FWS: TipwWSClient;
|
||||
FAPIClient: TAPIClient;
|
||||
FOnDonate: TOnDonateEvent;
|
||||
FOnStatus: TOnStatusEvent;
|
||||
FWsstoken: string;
|
||||
FWSID: 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);
|
||||
function ExtractValue(const T_, Text, _T: string): string;
|
||||
procedure HandleIncomingData(const Data: string);
|
||||
public
|
||||
constructor Create;
|
||||
destructor Destroy; override;
|
||||
procedure Connect(const WSSURL: string);
|
||||
procedure Disconnect;
|
||||
procedure Send(const Data: string);
|
||||
property OnDonate: TOnDonateEvent read FOnDonate write FOnDonate;
|
||||
property OnStatus: TOnStatusEvent read FOnStatus write FOnStatus;
|
||||
property Wsstoken: string read FWsstoken write FWsstoken;
|
||||
property WSID: string read FWSID write FWSID;
|
||||
property APIClient: TAPIClient read FAPIClient write FAPIClient;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
constructor TWSClient.Create;
|
||||
begin
|
||||
FWS := TipwWSClient.Create(nil);
|
||||
FWS.OnDataIn := DataIn;
|
||||
FWS.OnConnectionStatus := ConnectionStatus;
|
||||
FWS.OnError := Error;
|
||||
|
||||
end;
|
||||
|
||||
destructor TWSClient.Destroy;
|
||||
begin
|
||||
FWS.Disconnect;
|
||||
FWS.Free;
|
||||
inherited;
|
||||
end;
|
||||
|
||||
procedure TWSClient.Disconnect;
|
||||
begin
|
||||
FWS.Disconnect;
|
||||
end;
|
||||
|
||||
procedure TWSClient.Connect(const WSSURL: string);
|
||||
begin
|
||||
FWS.ConnectTo(WSSURL);
|
||||
end;
|
||||
|
||||
procedure TWSClient.Send(const Data: string);
|
||||
begin
|
||||
FWS.SendText(Data);
|
||||
end;
|
||||
|
||||
procedure TWSClient.DataIn(Sender: TObject; DataFormat: Integer;
|
||||
const Text: string; const TextB: TBytes; EOM, EOL: Boolean);
|
||||
begin
|
||||
HandleIncomingData(Text);
|
||||
// FWS.Ping;
|
||||
end;
|
||||
|
||||
procedure TWSClient.ConnectionStatus(Sender: TObject;
|
||||
const ConnectionEvent: string; StatusCode: Integer;
|
||||
const Description: string);
|
||||
begin
|
||||
if Assigned(FOnStatus) then
|
||||
FOnStatus(ConnectionEvent,StatusCode);
|
||||
end;
|
||||
|
||||
procedure TWSClient.Error(Sender: TObject; ErrorCode: Integer;
|
||||
const Description: string);
|
||||
begin
|
||||
// fLog.toLog(2, 'uWSDA', 'Error', 'Code: ' + IntToStr(ErrorCode) + ' - ' +
|
||||
// Description);
|
||||
end;
|
||||
|
||||
function TWSClient.ExtractValue(const T_, Text, _T: string): string;
|
||||
var
|
||||
StartPos, EndPos: Integer;
|
||||
begin
|
||||
StartPos := Pos(T_, Text);
|
||||
if StartPos = 0 then
|
||||
Exit('');
|
||||
StartPos := StartPos + Length(T_);
|
||||
EndPos := PosEx(_T, Text, StartPos);
|
||||
if EndPos = 0 then
|
||||
Exit('');
|
||||
Result := Copy(Text, StartPos, EndPos - StartPos);
|
||||
end;
|
||||
|
||||
procedure TWSClient.HandleIncomingData(const Data: string);
|
||||
var
|
||||
JSON: TJSONObject;
|
||||
jo: TJSONObject;
|
||||
DataObj: TJSONObject;
|
||||
DonationData: TJSONObject;
|
||||
ChannelArray: TJSONArray;
|
||||
wsstoken2: string;
|
||||
begin
|
||||
// fLog.toLog(3, 'uWSDA', '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
|
||||
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 }');
|
||||
end;
|
||||
end;
|
||||
// Îáðàáîòêà äîíàòîâ
|
||||
if Pos('"name":"Donations"', Data) > 0 then
|
||||
begin
|
||||
// fLog.toLog(3, 'uWSDA', 'HandleIncomingData', 'Íîâûé Äîíàò');
|
||||
JSON := TJSONObject.ParseJSONValue(Data) as TJSONObject;
|
||||
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'));
|
||||
finally
|
||||
JSON.Free;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
end.
|
||||
Reference in New Issue
Block a user