#1 2016-01-19 16:24:49

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Need help on WebSockets client low level usage

Hi Arnaud,

I'm new to WebSockets and I'm lost in the great amount of classes defined in SynBidirSock.pas tongue

I want to do something using the client class like the following (taken from https://github.com/andremussche/DelphiWebsockets):

  client := TIdHTTPWebsocketClient.Create(Self);
  client.Port := 12345;
  client.Host := 'localhost';
  client.SocketIOCompatible := True;
  client.SocketIO.OnEvent(C_SERVER_EVENT,
    procedure(const ASocket: ISocketIOContext; const aArgument: TSuperArray; const aCallback: ISocketIOCallback)
    begin
      ShowMessageInMainthread('Data PUSHED from server: ' + aArgument[0].AsJSon);
      //server wants a response?
      if aCallback <> nil then
        aCallback.SendResponse('thank for the push!');
    end);
  client.Connect;
  client.SocketIO.Emit(C_CLIENT_EVENT, SO([ 'request', 'some data']),
    //provide callback
    procedure(const ASocket: ISocketIOContext; const aJSON: ISuperObject; const aCallback: ISocketIOCallback)
    begin
      //show response (threadsafe)
      ShowMessageInMainthread('RESPONSE: ' + aJSON.AsJSon);
    end);

Which class in the mORMot framework I should use? I guess its

THttpClientWebSockets

?
And How to send and receive JSON data? Thanks.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#2 2016-01-19 16:32:10

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

Re: Need help on WebSockets client low level usage

Why do you want to use TIdHTTPWebsocketClient ?
I do not understand your problem.

Did you take a look at the documentation, and the samples?
See http://synopse.info/files/html/Synopse% … l#TITL_150
and https://github.com/synopse/mORMot/tree/ … WebSockets

Offline

#3 2016-01-20 03:48:56

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: Need help on WebSockets client low level usage

Hi ab,

I need to communicate with the Chrome browser via WebSockets: https://developer.chrome.com/devtools/d … r-protocol


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#4 2016-01-20 07:55:37

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

Re: Need help on WebSockets client low level usage

See the first sample detailed in the doc.
https://github.com/synopse/mORMot/blob/ … Server.dpr
There is a html page to work with a browser as a client
https://github.com/synopse/mORMot/blob/ … erver.html

Offline

#5 2016-01-20 08:40:37

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: Need help on WebSockets client low level usage

Thanks ab, so I'm trying THttpClientWebSockets, according your latest reply it seems that I'm not using the correct class, so will try TWebSocketServer now...

You can ignore my current testing result:  using THttpClientWebSockets:

var
  myStr: string;
begin
  if Assigned(FWSCli) then
    FWSCli.Free;

  //FWSCli := TSQLHttpClientWebsockets.Create(cSvrAddr, cCefDebugPort, TSQLModel.Create([])); //same error.
  FWSCli := THttpClientWebSockets.Open(cSvrAddr, cCefDebugPort);
  FWSCli.Settings.SetFullLog;

  myStr := FWSCli.WebSocketsUpgrade('ws://localhost:9289/someUri', '', True, True);
  LogIt('WebSocketsUpgrade result: ' + myStr);

Result:

WebSocketsUpgrade result: Invalid HTTP Upgrade Header

Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#6 2016-10-13 01:51:02

daisutao
Member
Registered: 2015-05-08
Posts: 17

Re: Need help on WebSockets client low level usage

It is so strange, there is a class named TWebSocketServer, but no class TWebSocketClient.

the client demo is a html page, but i want write client with delphi, how?

Offline

#7 2016-10-13 06:32:47

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,539
Website

Re: Need help on WebSockets client low level usage

You have a THttpClientWebSockets class for this

Offline

#8 2016-10-13 07:56:52

daisutao
Member
Registered: 2015-05-08
Posts: 17

Re: Need help on WebSockets client low level usage

i guess it is, but i don't known how to use it .

Offline

#9 2016-10-13 08:05:59

daisutao
Member
Registered: 2015-05-08
Posts: 17

Re: Need help on WebSockets client low level usage

i guess it is, but i don't known how to use it .

Offline

Board footer

Powered by FluxBB