Jump to content
Sign in to follow this  
What202

W3webSocket

Recommended Posts

uses W3C.DOM, SmartCL.Net.websocket;

 

FSocket.Connect("your url here", []);

FSocket.OnOpen:= Procedure (Sender:TW3WebSocket)
  begin
    writeln("Socket connected");
    Sender.Write("your data here");
  end;

FSocket.OnClosed:=procedure (Sender:TW3WebSocket)
  begin
    Writeln("Socket disconnected");
  end;

FSocket.OnMessage:= Procedure (Sender: TW3WebSocket; Message: TWebSocketMessageData)
  begin
    Writeln("Data received:" + Message.mdText);
  end;

FSocket.OnError:= procedure (Sender: TW3WebSocket; const Error: JEvent)
  begin
    writeln("Socket has errors");
  end;

 

 

Share this post


Link to post
Share on other sites
On 28/3/2018 at 3:55 AM, IElite said:

uses W3C.DOM, SmartCL.Net.websocket;

 

FSocket.Connect("your url here", []);

FSocket.OnOpen:= Procedure (Sender:TW3WebSocket)
  begin
    writeln("Socket connected");
    Sender.Write("your data here");
  end;

FSocket.OnClosed:=procedure (Sender:TW3WebSocket)
  begin
    Writeln("Socket disconnected");
  end;

FSocket.OnMessage:= Procedure (Sender: TW3WebSocket; Message: TWebSocketMessageData)
  begin
    Writeln("Data received:" + Message.mdText);
  end;

FSocket.OnError:= procedure (Sender: TW3WebSocket; const Error: JEvent)
  begin
    writeln("Socket has errors");
  end;

 

 

Hi.

I try a similar example, but don't work in 3.0,0.201 version.

var FSocket: TW3WebSocket; //< OK
FSocket := TW3WebSocket.Create; //< OK
FSocket.Connect('http://192.168.1.170:8085/api/myurl', [], ????); //< New parameter

How to implement New parameter?

Thx.

 

Share this post


Link to post
Share on other sites

you should have

    procedure   Connect(URL: string; Protocols: array of string; const CB: TWebSocketCallback);
    procedure   Connect(URL: string; Protocols: array of string); overload;
    procedure   Connect(URL: string); overload;

 

Share this post


Link to post
Share on other sites

I have not updated,I have done a new instalation with the 3.0.0.201

In the uses area:

System.Types,
  System.Types.Convert,
  System.Objects,
  System.Time,
  SmartCL.System,
  SmartCL.Time,
  SmartCL.Graphics,
  SmartCL.Components,
  SmartCL.FileUtils,
  SmartCL.Forms,
  SmartCL.Fonts,
  SmartCL.Theme,
  SmartCL.Borders,
  SmartCL.Application, SmartCL.Controls.Label, SmartCL.Controls.EditBox,
  SmartCL.Controls.Button,

  SmartCL.Layout,
  System.Encoding,
  System.JSON,
  SmartCL.net.http,
  SmartCL.net.Rest,
  SmartCL.Net.websocket;

I use it like that:

var FSocket: TW3WebSocket;
FSocket := TW3WebSocket.Create;
FSocket.Connect('http://192.168.1.170:8085/api/pedidoslogistic',[]); //does not compile

if i try to

FSocket.Connect('http://192.168.1.170:8085/api/pedidoslogistic',[],nil); //Complile but error in execution

FSocket.Connect('http://192.168.1.170:8085/api/pedidoslogistic',); //does not compile

I'm missing some unit? Can not the funcions be overloaded in RTL?

This code does not compile either (error with JEvent):

FSocket.OnError:= procedure (Sender: TW3WebSocket; const Error: JEvent)
  begin
    writeln("Socket has errors");
  end;

 

 

 

Share this post


Link to post
Share on other sites
4 hours ago, IElite said:

you should have

    procedure   Connect(URL: string; Protocols: array of string; const CB: TWebSocketCallback);
    procedure   Connect(URL: string; Protocols: array of string); overload;
    procedure   Connect(URL: string); overload;

 

I have not updated,I have done a new instalation with the 3.0.0.201

In the uses area:

System.Types,
  System.Types.Convert,
  System.Objects,
  System.Time,
  SmartCL.System,
  SmartCL.Time,
  SmartCL.Graphics,
  SmartCL.Components,
  SmartCL.FileUtils,
  SmartCL.Forms,
  SmartCL.Fonts,
  SmartCL.Theme,
  SmartCL.Borders,
  SmartCL.Application, SmartCL.Controls.Label, SmartCL.Controls.EditBox,
  SmartCL.Controls.Button,

  SmartCL.Layout,
  System.Encoding,
  System.JSON,
  SmartCL.net.http,
  SmartCL.net.Rest,
  SmartCL.Net.websocket;

I use it like that:

var FSocket: TW3WebSocket;
FSocket := TW3WebSocket.Create;
FSocket.Connect('http://192.168.1.170:8085/api/pedidoslogistic',[]); //does not compile

if i try to

FSocket.Connect('http://192.168.1.170:8085/api/pedidoslogistic',[],nil); //Complile but error in execution

FSocket.Connect('http://192.168.1.170:8085/api/pedidoslogistic',); //does not compile

I'm missing some unit? Can not the funcions be overloaded in RTL?

This code does not compile either (error with JEvent):

FSocket.OnError:= procedure (Sender: TW3WebSocket; const Error: JEvent)
  begin
    writeln("Socket has errors");
  end;

I have not updated,I have done a new instalation with the 3.0.0.201

In the uses area:

System.Types,
  System.Types.Convert,
  System.Objects,
  System.Time,
  SmartCL.System,
  SmartCL.Time,
  SmartCL.Graphics,
  SmartCL.Components,
  SmartCL.FileUtils,
  SmartCL.Forms,
  SmartCL.Fonts,
  SmartCL.Theme,
  SmartCL.Borders,
  SmartCL.Application, SmartCL.Controls.Label, SmartCL.Controls.EditBox,
  SmartCL.Controls.Button,

  SmartCL.Layout,
  System.Encoding,
  System.JSON,
  SmartCL.net.http,
  SmartCL.net.Rest,
  SmartCL.Net.websocket;

I use it like that:

var FSocket: TW3WebSocket;
FSocket := TW3WebSocket.Create;
FSocket.Connect('http://192.168.1.170:8085/api/pedidoslogistic',[]); //does not compile

if i try to

FSocket.Connect('http://192.168.1.170:8085/api/pedidoslogistic',[],nil); //Complile but error in execution

FSocket.Connect('http://192.168.1.170:8085/api/pedidoslogistic',); //does not compile

I'm missing some unit? Can not the funcions be overloaded in RTL?

This code does not compile either (error with JEvent):

FSocket.OnError:= procedure (Sender: TW3WebSocket; const Error: JEvent)
  begin
    writeln("Socket has errors");
  end;

 

Share this post


Link to post
Share on other sites

It compiles  just fine for me on 3.0.0.209

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components

 var FSocket: TW3WebSocket;
FSocket := TW3WebSocket.Create;
FSocket.Connect('http://192.168.1.170:8085/api/pedidoslogistic',[]);

end;

[INFO] Building project 'test'...
[INFO] Compilation successful [166 ms]
[INFO] Generating JavaScript successful [585 ms]
[INFO] Linking successful [50 ms]
 

Share this post


Link to post
Share on other sites

This one compiles and runs without error for me as well

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components

 var FSocket: TW3WebSocket;
FSocket := TW3WebSocket.Create;
FSocket.Connect('http://192.168.1.170:8085/api/pedidoslogistic',[],nil)

end;

[INFO] Building project 'test'...
[INFO] Compilation successful [227 ms]
[INFO] Generating JavaScript successful [553 ms]
[INFO] Linking successful [31 ms]
 

Share this post


Link to post
Share on other sites

I have uninstalled and then instaled to update to version 3.0.0.209.

the line 

FSocket.Connect('http://192.168.1.170:8085/api/myurl');

now compile, but don´t work.

show my code:

type
TMyJsonCommand = record
    Tabla: String;
    Codigo: String;
    Clave: String;
  end;


procedure TForm1.W3Button3Click(Sender: TObject);
var JsonToSend: TMyJsonCommand;
begin
var FSocket: TW3WebSocket;
FSocket := TW3WebSocket.Create;
FSocket.OnOpen:= Procedure (Sender:TW3WebSocket)
  begin
    showmessage('Socket connected');
    Sender.Write("your data here");
  end;

FSocket.OnClosed:=procedure (Sender:TW3WebSocket)
  begin
    showMessage("Socket disconnected");
  end;

FSocket.OnMessage:= Procedure (Sender: TW3WebSocket; Message: TWebSocketMessageData)
  begin
    showMessage("Data received:" + Message.mdText);
  end;

FSocket.OnError:= procedure (Sender: TW3WebSocket; const Error: JEvent)
  begin
    showmessage("Socket has errors");
  end;

FSocket.Connect('http://192.168.1.170:8085/api/myurl');

end;

Share this post


Link to post
Share on other sites

Mine runs fine! 

See my code below.  If yours does not work, you need to uninstall and re-install.

First ,   Make sure you first save your projects folder (any work that is not part of SMS install)  and your license (user.lic) file

Next. 

Uninstall any versions you have installed AND/OR remove any old install locations 

 delete all remaining occurrences of 

C:\Program Files (x86)\Smart Mobile Studio
C:\users\<username>\My Documents\Smart Mobile Projects
C:\ProgramData\The Smart Company\Smart Mobile Studio

Next

Re-install using : http://smartmobilestudio.com/alpha.zip  (just unzip folder)

Next

Run the SmartUpdate.exe

 

unit Form1;

interface

uses 
  W3C.DOM,
  System.Types,
  System.Types.Convert,
  System.Objects,
  System.Time,
  SmartCL.Net.websocket,
  SmartCL.System,
  SmartCL.Time,
  SmartCL.Graphics,
  SmartCL.Components,
  SmartCL.FileUtils,
  SmartCL.Forms,
  SmartCL.Fonts,
  SmartCL.Theme,
  SmartCL.Borders,
  SmartCL.Application,
  SmartCL.Controls.Button;

type
  TForm1 = class(TW3Form)
    procedure W3Button1Click(Sender: TObject);
  private
    {$I 'Form1:intf'}
    FSocket : TW3WebSocket;
  protected
    procedure InitializeForm; override;
    procedure InitializeObject; override;
    procedure Resize; override;
  end;

implementation

{ TForm1 }

procedure TForm1.W3Button1Click(Sender: TObject);
begin
  FSocket.Connect("wss://echo.websocket.org",[],nil);
end;

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components
end;

procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}
  FSocket := TW3WebSocket.Create;

  FSocket.OnOpen:= Procedure (Sender:TW3WebSocket)
  begin
    ShowMessage("Socket connected");
    Sender.Write("your data here");
  end;

  FSocket.OnClosed:=procedure (Sender:TW3WebSocket)
  begin
    ShowMessage("Socket disconnected");
  end;

  FSocket.OnMessage:= Procedure (Sender: TW3WebSocket; Message: TWebSocketMessageData)
  begin
    ShowMessage("Data received:" + Message.mdText);
  end;

  FSocket.OnError:= procedure (Sender: TW3WebSocket; const Error: JEvent)
  begin
    ShowMessage("Socket has errors");
  end;
end;
 
procedure TForm1.Resize;
begin
  inherited;
end;
 
initialization
  Forms.RegisterForm({$I %FILE%}, TForm1);
end.

 

Share this post


Link to post
Share on other sites

I have deleted all the folders and saved the license. I have unzipped the zip file and run smartupdate. The only available channel was ALPHA and it has been updated to 2.9.9.125. I have run smartupdate again. The only channel was still ALFA. I modified the file Settings.ini and changed the channel to BETA and re-executed. 12 files have been updated and the version is now 3.0.0.209. I have run again and it still did not work. BUT, 

I have captured the exception and the error was in the protocol: the address http: // xxxx was not valid. I changed to ws: // xxx and it works (nothing to do with the rtl).:)

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×