Skip to content

Registering callback on external socket #25

@dneuschaefer-rube

Description

@dneuschaefer-rube

I'm trying to register a callback on an external socket. I've got working js code that does just that:

const WebSocket = require('ws');

let authToken = '...';
let user = 'user';
let password = 'pw';
let port = 12345;

let ws = new WebSocket(`ws://${user}:${password}@127.0.0.1:${port}/`, "wamp", {
  origin: `http://127.0.0.1:${port}`,
  Host: `127.0.0.1:${port}`,
  Authorization: `Basic ${authToken}`
});

ws.on('message', (msg) => {
  console.log(msg);
});

ws.on('open', () => {
  ws.send('[5, "OnJsonApiEvent"]');
});

I'm trying to replicate this in Delphi with your library, but I'm not really sure how to use it, neither to establish the connection, nor to do what the "on" calls do.

This is what I have so far, but it does nothing:

procedure TestSocket;
  var client: TIdHTTPWebsocketClient;
begin
  client := TIdHTTPWebsocketClient.Create(Self);
  client.Port := 12345;
  client.Host := '127.0.0.1';
  client.Request.CustomHeaders.AddValue('Origin', 'http://127.0.0.1:12345');
  client.Request.BasicAuthentication := true;
  client.Request.Username := 'user';
  client.Request.Password := 'pw';
  client.SocketIOCompatible := True;

  client.SocketIO.OnSocketIOMsg :=
    procedure(const ASocket: ISocketIOContext; const text: string; const aCallback: ISocketIOCallback)
    begin
      // maybe this is how to register the callback like ws.on('message', ...) ?
      Logfile.Log(text);
    end;

  client.SocketIO.OnEvent('message',
    procedure(const ASocket: ISocketIOContext; const aArgument: TSuperArray; const aCallback: ISocketIOCallback)
    begin
      // or like this ?
      Logfile.Log(aArgument[0].AsString);
    end);

  client.SocketIO.OnEvent('open',
    procedure(const ASocket: ISocketIOContext; const aArgument: TSuperArray; const aCallback: ISocketIOCallback)
    begin
      // this is not called
      client.SocketIO.Send('[5, "OnJsonApiEvent"]');
    end);

  client.Connect;
end;

Do you see anything wrong with that? Any help would be greatly appreciated :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions