Anonymous User
  Monday, 23 March 2020
  22 Replies
  4.5K Visits
  Subscribe
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.
5 years ago
·
#251
Even with the change it still doesn't work.

Did you test this?
5 years ago
·
#250
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.
  • Page :
  • 1
  • 2
There are no replies made for this post yet.
Submit Your Response
Upload files or images for this discussion by clicking on the upload button below.
Supported: gif,jpg,png,jpeg,zip,rar,pdf,pas,dfm,dpr,dproj,dpk
· Insert · Remove
  Upload Files (Maximum 10MB)