#1 2021-01-31 11:13:51

RaelB
Member
Registered: 2010-08-04
Posts: 57

Simple WebSocket client example

I'm trying to get up and running with a simple Websocket client example.

I would like to connect to the test echo websocket server @
wss://echo.websocket.org

(info at https://www.websocket.org/echo.html)

My code looks like this:

...
    fWebSockets: THttpClientWebSockets;
    proto: TWebSocketProtocolChat;

    procedure OnFrameReceived(Sender: THttpServerResp;
      const Frame: TWebSocketFrame);
...
procedure TForm1.btnConnect2Click(Sender: TObject);
var
  msg: RawUTF8;
begin

  fWebSockets := THttpClientWebSockets.Create;
  fWebSockets.Open('echo.websocket.org', '80');
  proto := TWebSocketProtocolChat.Create('echo', '', OnFrameReceived);

  msg := fWebSockets.WebSocketsUpgrade('' {A}, '', True, false, proto);

  if msg = '' then
    Memo1.Lines.Add( 'ConnectToWebSockets: upgraded with protocol "EchoTest"')
  else
    Memo1.Lines.Add('Error: WebSocketsUpgrade: ' + msg);
end;

I keep on getting the result  "Error: WebSocketsUpgrade: Invalid HTTP Upgrade Header".

I tried different things in {A}, e.g. echo.websocket.org, or wss://echo.websocket.org, I still get the same result.

Can anyone help me with this?

Thanks

Offline

#2 2021-01-31 13:29:56

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

Re: Simple WebSocket client example

Start from the supplied example and associated HTML page.

Offline

Board footer

Powered by FluxBB