By Guest on Tuesday, 03 March 2020
Posted in General
Replies 5
Likes 0
Views 2.8K
Votes 0
Hi, I am testing your framework.

I have a nodejs app with socket.io server in localhost, port 3001.

I created a new delphi project and I used:

with sgcWebSocketClient_SocketIO1 do begin
Host:='http://localhost';
Port:=3001;
SendConnect('/?my=param');
SendEvent('new-user','luiz');
end;

But the server doesn´t receive nothing.

What am II doing wrong?
Hi,

Host is a property where you must set domain name or ip, not URL, so in this case, you must set:

Host := 'localhost';
Port := 3001;

If you want pass parameters, you can use Options.Parameters := '/your_params';

Another option is not use host, port.. and pass full url, so component will decode url and set host, port, params... automatically, Example:


TsgcWebSocketClient.URL := 'ws://localhost:3001';


Setting URL to this value will have the same effect that setting Host and port.

TsgcWebSocketClient_SocketIO component is deprecated, use instead TsgcWebSocketClient with TsgcWSAPI_SocketIO attached to TsgcWebSocketClient, there is a demo inside demo folder called client which allows to connect to several servers, one of them is Socket.IO

SendEvent, SendConnect... are methods deprecated which only applies to socket.io server api 0.*, now socket.io has newer undocumented api which is not compatible with these methods. So you must use WriteData method to send JSON messages.

Example: this method calls to method 'add user' and pass as parameter 'UserName'


TsgcWSAPI_SocketIO.WriteData('42["add user", "UserName"]');


Hope this helps.

Sergio
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Hi,


TsgcWebSocketClient.URL := 'ws://localhost:3001';


Sergio


Thank you.
Now I can connect.
But I have a problem. I need a persistent connection between client and server.
I noted after some seconds the client is disconnected.

If I use an html page with socket.io cliente js, this is not happend.

But with VCL, it is disconnected after some seconds.

I see watchdog poperty to make a reconnect, but It doesn´t solve my problem because during the time I am disconnected I lost messages sent from server.

Is there some client option to stay always connected?

Regards, Luiz
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
There is a property called heartbeat which sends a ping every x seconds.
Example: to send a ping every 10 seconds


Client.HeartBeat.Enabled := True;
Client.HeartBeat.Interval := 10;
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Hi, ia m sending this message to server.
The server receive the message, but I can´t receive the return message from server.


WSClientIO.WriteData('42["send-chat-message", "chat mess"]');


//server
socket.on('send-chat-message', message => {

console.log(message)
socket.broadcast.emit('chat-message', "Hi")
})
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Hi,

If you receive message from client, the process to send a message from server to client is done by socket.io server. Client component only dispatch messages to OnMessage event of TsgcWebSocketClient. I see you are mixing single and double quote in emit method, I don't know if this affects. Check socket.io docs and if still not works send me your socket.io server sample and I will to test in my machine.

Sergio
·
4 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
View Full Post