Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 7 of 7 total
Thread Websocket?
Mon, Dec 19 2022 6:05 PMPermanent Link

marcusf

Are there any examples on how to use the TSocket / TWebSocket components?
Tue, Dec 20 2022 10:50 AMPermanent Link

marcusf

marcusf wrote:

Are there any examples on how to use the TSocket / TWebSocket components?

I'm using the esegece websocket library on the server.
Wed, Dec 21 2022 9:07 AMPermanent Link

Raul

Team Elevate Team Elevate

<<marcusf wrote:

Are there any examples on how to use the TSocket / TWebSocket components?
>>

Have not tried in a while but in a nutshell you'd need to do following:

1. Current WebSock unit has couple of small bugs - you need to fix up GetURL and GetProtocol functions and return "Result:='';" (they currently use Result:=False for 2nd half of ifdef);

2. Then add unit to library as i don't believe it's there by default - i.e. Library->Add to Library and then use TSocket as class name and unit file should point to fixed up WebSock.wbs unit

3. Make sure library reloads - i'd restart IDE

4. Now you should have a new TSocket component in the designer and can drop it on form etc

5. socket "Open" opens connection - specify url and port (i.e. Socket1.Open('ws://192.168.0.1:8001);

6. OnMessage handler should fire for incoming data messages

7. socket "send" sens a message you specify on existing connection

Raul
Wed, Dec 21 2022 10:49 AMPermanent Link

marcusf

<<Raul wrote:

<<marcusf wrote:

Are there any examples on how to use the TSocket / TWebSocket components?
>>

Have not tried in a while but in a nutshell you'd need to do following:

1. Current WebSock unit has couple of small bugs - you need to fix up GetURL and GetProtocol functions and return "Result:='';" (they currently use Result:=False for 2nd half of ifdef);

2. Then add unit to library as i don't believe it's there by default - i.e. Library->Add to Library and then use TSocket as class name and unit file should point to fixed up WebSock.wbs unit

3. Make sure library reloads - i'd restart IDE

4. Now you should have a new TSocket component in the designer and can drop it on form etc

5. socket "Open" opens connection - specify url and port (i.e. Socket1.Open('ws://192.168.0.1:8001);

6. OnMessage handler should fire for incoming data messages

7. socket "send" sens a message you specify on existing connection

Raul
>>

Thanks Raul, I've done that but I'm getting an odd error when trying to open().
When I use Run, in EWB I get "SecurityError" and in Chrome pointed at the same url I get "SyntaxError: Failed to construct 'WebSocket': The SubProtocol '' is invalid"
Wed, Dec 21 2022 11:12 AMPermanent Link

marcusf

I got it working Smile

I had to make a change to webdom.wbs as well.

I added: constructor Create(const url: String); to the  external TWebSocket emit WebSocket  = class public section and added this to the TSocket class

procedure TSocket.Open(const URL: String);
begin
  {$IFDEF BROWSER}
  FWebSocket:=TWebSocket.Create(Url);
  Initialize;
  {$ENDIF}
end;

And now it connects and I successfully sent a test message.
Wed, Dec 21 2022 11:20 AMPermanent Link

marcusf

Interestingly though, I still get the error in EWB but it works in Chrome.
Also the events do not seem to work. I can see the data in Chrome inspector but onConnected and onMessage does not trigger.
Wed, Dec 21 2022 12:32 PMPermanent Link

marcusf

This is an interesting puzzle.

So the SecurityError likely comes from the embedded IE browser. A recent update doesn't like to open local websocket connections.

Not sure why the events werent firing but when I use TWebSocket directly (Not TSocket) and manually assign event handlers, it works fine so I'll go that route for now.

I'll keep going with this and see if I run into any other issues.
Image