Registering callback on external socket
dneuschaefer-rube opened this issue · 2 comments
dneuschaefer-rube commented
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 :)
edsonalvesan commented
Hi, did you solve your problem? Am I going through the same situation?
charoleizer commented
already tried this?
client.SocketIO.OnEvent('message',
procedure(const ASocket: ISocketIOContext; const aArgument: TSuperArray; const aCallback: ISocketIOCallback)
begin
if aCallback <> nil then
aCallback.SendResponse('[5, "OnJsonApiEvent"]')
end);