itisnajim / SocketIOUnity

A Wrapper for socket.io-client-csharp to work with Unity.
MIT License
393 stars 67 forks source link

how to maintain the connection (Ping-pong/heartbeat) #8

Closed JoyX2 closed 2 years ago

JoyX2 commented 2 years ago

How to keep the client connected ? I'm using KyleDulce.SocketIo but it sadly got depreciated, and as soon as I developed deep enough I realized that there is no ping-pong/heartbeeat mechanism, so all clients would disconnected as soon as the time ran out.

could you show me an example to maintain a heart beat ? I'm using python as the backenet/server, would that be a problem ?

itisnajim commented 2 years ago

To debug the client ping/pong and its connectivity try this:

client.OnConnected += (sender, e) => {
  Debug.Log("Client_OnConnected");
};
client.OnPing += (sender, e) => {
  Debug.Log("Ping");
};
client.OnPong += (sender, e) => {
  Debug.Log("Pong: " + e.TotalMilliseconds);
};
client.OnDisconnected += (sender, e) => {
  Debug.Log("disconnect: " + e);
};
client.OnReconnectAttempt += (sender, e) => {
  Debug.Log($"{DateTime.Now} Reconnecting: attempt = {e}");
};
client.OnAny((name, response) => {
  Debug.Log("Client_OnAny: event name: " + name + " response: " + response.GetValue().GetRawText());
});

always refer to the main package for examples, these links can help: https://github.com/itisnajim/SocketIOUnity/issues/5 https://github.com/doghappy/socket.io-client-csharp/blob/master/src/SocketIOClient.Sample/Program.cs https://github.com/itisnajim/SocketIOUnity#initiation

itisnajim commented 2 years ago

Because no comment left in a long time ... i'm going to close this !