#1 2022-03-11 14:54:14

touchvip
Member
Registered: 2022-03-11
Posts: 5

websocket under deepin linux

Use mORmot 2.0

unit uMainServer;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  mormot.net.relay,
  mormot.net.ws.core,
  mormot.net.server,
  mormot.net.ws.async,
  mormot.net.ws.server;

type

  { TWebSocketProtocolEcho }

  TWebSocketProtocolEcho = class(TWebSocketProtocolChat)
  protected
    procedure EchoFrame(Sender: TWebCrtSocketProcess; const Frame: TWebSocketFrame);
  end;

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FServer:TWebSocketServer;
    procedure AddLog(sLog:String);

  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TWebSocketProtocolEcho }

procedure TWebSocketProtocolEcho.EchoFrame(Sender: TWebCrtSocketProcess;
  const Frame: TWebSocketFrame);
begin
  case Frame.opcode of
    focContinuation:
        Form1.AddLog('Connected');
    focConnectionClose:
      Form1.AddLog('Disconnected');
    focText,focBinary:
      begin
       Form1.AddLog('Echoing: '+IntToStr(length(Frame.payload))+' bytes');
       SendFrame(Sender,Frame);
    end;
  end;

end;

{ TForm1 }

procedure TForm1.Button3Click(Sender: TObject);
begin
  Close;
end;

procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  FServer.Suspended:=False;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  FServer.Suspended:=True;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  protocol: TWebSocketProtocolEcho;
begin
  FServer := TWebSocketServer.Create('8888',nil,nil,'test',2,90000000 );
  protocol := TWebSocketProtocolEcho.Create('meow','');
  protocol.OnIncomingFrame := @protocol.EchoFrame;
  FServer.WebSocketProtocols.Add(protocol);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin

end;

procedure TForm1.AddLog(sLog: String);
begin
  Memo1.Lines.Add(sLog);
end;



end.
            

The above code is no problem to use under windows, and the server stops after sending a few messages under deepin linux.

Offline

#2 2022-03-11 16:45:42

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: websocket under deepin linux

Please follow the forum rules and don't put code in the forum thread directly.

Then please don't create LCL / UI apps, but a short reproducible console app.

Offline

#3 2022-03-12 01:29:03

touchvip
Member
Registered: 2022-03-11
Posts: 5

Re: websocket under deepin linux

OK, thank you for your reply

Offline

Board footer

Powered by FluxBB