I have now been back and forth a bit with the code, but there are issues I cannot easily solve here.
First thing first, a tip: "TWebSocketCommunincator", should this not be "TWebSocketCommunicator"? (Just a typo, maybe intended, I don't know.)
-
Then my big problem.
First my code:
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
wsutils,
wsmessages,
wsstream,
ssockets,
WebsocketsClient;
type
{ TForm1 }
TForm1 = class(TForm)
btnSubscribe: TButton;
btnUnsubscribe: TButton;
edtPort: TEdit;
edtSubscribeAddress: TEdit;
mmoResult: TMemo;
mmoSubscriptionInfo: TMemo;
MessageTimer: TTimer;
procedure btnSubscribeClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure MessageTimerTimer(Sender: TObject);
private
MyClient : TWebsocketClient;
MyStream : TWebSocketCommunincator;//TStream;
procedure HandleClose(sender: TObject);
public
end;
var
Form1: TForm1;
MyStreamVar : TLockedSocketStream;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.HandleClose(Sender: TObject);
begin
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MyStream.Create(MyStreamVar,False,False);
MyClient := TWebsocketClient.Create('stream.binance.com', 9443, '/');
MyStream := MyClient.Connect(TSocketHandler.Create);// GetDefaultHandler
MyStream.OnClose := @HandleClose; // Handle close is a procedure HandleClose(sender: TObject); in your class
MyStream.StartReceiveMessageThread; // handle all incoming messages
end;
procedure TForm1.btnSubscribeClick(Sender: TObject);
VAR
MyResponse : TWebSocketStringMessage;
MyTextData : UTF8String;
begin
{
MyTextData:='';
MyResponse.Create(MyTextData);
MyStream.WriteStringMessage(mmoSubscriptionInfo.Lines.Text);// 'JSON subscribe request'
MyResponse := MyStream.WaitForStringMessage;
try
// Check if response.Data contains the expected response
finally
MyResponse.Free;
end;
}
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
MyStream.Free;
MyClient.Free;
end;
procedure TForm1.MessageTimerTimer(Sender: TObject);
var
Messages: TWebsocketMessageOwnerList;
Message: TWebsocketMessage;
begin
{
Messages := TWebsocketMessageOwnerList.Create;
try
MyStream.GetUnprocessedMessages(Messages);
for Message in Messages do
// Update your UI with the contents of Message
mmoResult.Append(Message.ToString);
finally
Messages.Free;
end;
}
end;
end.
So far, I get no error on compile.
When running, I get SIGSEGV on: MyStream.OnClose := @HandleClose;
When I // don't do this line, I get SIGSEGV on the next line, MyStream.StartReceiveMessageThread;, which then point to line 645 in the wsstream-unit: if not Assigned(FReceiveMessageThread) then...
There just seems to be an endless list of things popping up here.
Maybe because I don't have the right units, maybe because I don't know how to initialize the variables properly.
I realize that this wasn't a walk in the park
But I don't want to give up
I don't even know if I need to install something for SSL-functionality.
Your example gives: TSSLSocketHandler
But I cannot seem to find this here.