By Guest on Monday, 23 March 2020
Posted in General
Replies 22
Likes 0
Views 3.7K
Votes 0
The websocket server console application is not running events.
But same sample in VCL application is ok.


program Server;

{$APPTYPE CONSOLE}

{$R *.res}

uses
System.SysUtils,
sgcWebSocket,
sgcWebSocket_Classes;

type
TServer = class
strict private
fServer: TsgcWebSocketServer;
procedure DoMessage(pConnection: TsgcWSConnection; const pText: string);
procedure DoConnection(pConnection: TsgcWSConnection);
public
procedure Start;
end;

{ TServer }

// it is not working???
procedure TServer.DoConnection(pConnection: TsgcWSConnection);
begin
Writeln('Servidor Conectado...');
end;

// it is not working???
procedure TServer.DoMessage(pConnection: TsgcWSConnection; const pText: string);
begin
Writeln(pText);
end;

procedure TServer.Start;
begin
Writeln('Servidor de Websocket ligado na porta 5000...');
fServer := TsgcWebSocketServer.Create(nil);
try
fServer.Port := 5000;
fServer.OnMessage := DoMessage;
fServer.OnConnect := DoConnection;
fServer.Active := True;
fServer.Start;
while fServer.Active do
begin
Sleep(10);
end;
finally
fServer.Free;
end;
end;

var
vServer: TServer;
begin
try
vServer := TServer.Create;
try
vServer.Start;
finally
vServer.Free;
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
Call to start server is called twice, instead of set Active := True and call Start method, set only Active := True.


procedure TServer.Start;
begin
Writeln('Servidor de Websocket ligado na porta 5000...');
fServer := TsgcWebSocketServer.Create(nil);
try
fServer.Port := 5000;
fServer.OnMessage := DoMessage;
fServer.OnConnect := DoConnection;
fServer.Active := True;
// fServer.Start; <-- commented this line
while fServer.Active do
begin
Sleep(10);
end;
finally
fServer.Free;
end;
end;


Doing this change, your application will work.
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Even with the change it still doesn't work.

Did you test this?
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Yes, I test your sample, check attached screenshot.

console_server.png
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Do you have any idea what's going on?

Thank you very much for the quick responses and commitment to helping me.
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Could you try to change server port? It's really weird that client connects and you don't get a notification in server side.
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Nothing yet...
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Please attach your compiled sample and your project source and I'll try later.
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Follow my example source code.
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
My compiled sample wasn't possible to send because here have attach limit (2mb).
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Hi,

Your sample source code works well, find attached your compiled project (just compress your exe with rar and you can upload).

Sergio
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Your executable example works well here.

Any restrictions for the version being the evaluation version? We are doing tests to acquire it.

My executable example has a different size than yours. Why?
My: 9,67MB
Your: 2,57MB

Attention:
My antivirus has detected a trojan in its executable. Sorry, the print is in Portuguese.
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Here is my compatible executable in 3 pieces.
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
No, there are no restrictions more than can run for a limited time.
This is a false positive, my antivirus doesn't detect nothing, from time to time I get false positives, don't worry about it.
I will install community and try to reproduce but I never hear something like this.

Your executable example works well here.

Any restrictions for the version being the evaluation version? We are doing tests to acquire it.

My executable example has a different size than yours. Why?
My: 9,67MB
Your: 2,57MB

Attention:
My antivirus has detected a trojan in its executable. Sorry, the print is in Portuguese.
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Yes, I get the same behaviour than you, I don't get any message.

Here is my compatible executable in 3 pieces.
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Which sgcWebSockets version do you use? seems is not latest.
If it's not latest trial, download latest, and try again.
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Try the following code:


program Server;

{$APPTYPE CONSOLE}

{$R *.res}

uses
System.SysUtils,
sgcWebSocket,
sgcWebSocket_Classes,
sgcWebSocket_Types;

type
TServer = class
strict private
fServer: TsgcWebSocketServer;
procedure DoMessage(pConnection: TsgcWSConnection; const pText: string);
procedure DoConnection(pConnection: TsgcWSConnection);
public
procedure Start;
end;

{ TServer }

procedure TServer.DoConnection(pConnection: TsgcWSConnection);
begin
Writeln('client connected...');
end;

procedure TServer.DoMessage(pConnection: TsgcWSConnection; const pText: string);
begin
Writeln(pText);
end;

procedure TServer.Start;
begin
Writeln('Servidor de Websocket ligado na porta 3001...');
fServer := TsgcWebSocketServer.Create(nil);
try
fServer.Port := 3001;
fServer.OnMessage := DoMessage;
fServer.OnConnect := DoConnection;
fServer.NotifyEvents := neNoSync;
fServer.Active := True;
//fServer.Start;
while fServer.Active do
begin
Sleep(10);
end;
finally
fServer.Free;
end;
end;

var
vServer: TServer;
begin
try
vServer := TServer.Create;
try
vServer.Start;
finally
vServer.Free;
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.

·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
My version is 4.3.5 TRIAL.

Which sgcWebSockets version do you use? seems is not latest.
If it's not latest trial, download latest, and try again.
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Yes sorry, my mistake, I sent you another code update, did you try? The problem is that when is a console application Notifyevents property must be neNoSync, this is done automatically on source version but not trial.

Kind Regards,
Sergio

My version is 4.3.5 TRIAL.

Which sgcWebSockets version do you use? seems is not latest.
If it's not latest trial, download latest, and try again.
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
I'm actually running into the same issue. I'm trying to use a DataModule to contain my components to spare the time of manually creating them. It seems to actually connect however the api events are not firing.

I have attached my project below. I have attempted to change all the Notify Events however it doesn't do anything.

I'm changing the Notify event at design time. I have cycled through all of them to test it.

Using Source Beta version: 4.3.6

Please see attached
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Hi,

I think you experience the same problem that Marcelo, set TsgcWebSocketClient.NotifyEvents := neNoSync and will work, at least works here.
I attach a screenshot:

hwid_reset.png


Sergio

I'm actually running into the same issue. I'm trying to use a DataModule to contain my components to spare the time of manually creating them. It seems to actually connect however the api events are not firing.

I have attached my project below. I have attempted to change all the Notify Events however it doesn't do anything.

I'm changing the Notify event at design time. I have cycled through all of them to test it.

Using Source Beta version: 4.3.6

Please see attached
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
View Full Post