Tuesday, 03 March 2020
  5 Replies
  2.5K Visits
  Subscribe
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?
4 years ago
·
#211
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
Attachments (1)
4 years ago
·
#216
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
·
#218
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;
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
·
#221
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
  • Page :
  • 1
There are no replies made for this post yet.
Submit Your Response
Upload files or images for this discussion by clicking on the upload button below.
Supported: gif,jpg,png,jpeg,zip,rar,pdf
· Insert · Remove
  Upload Files (Maximum 10MB)