I apologize, i couldn’t find solution in posted threads.
So my problem is:
I have big ping even on local machine (i didn’t test it yet on internet connection), 30+ to be exact.
I implemented simple PingPong/2.
So my code is (right now its all in one script):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using TMPro;
public class MyMessageTypes
{
public static short Ping = 1005;
public static short Pong = 1005;
};
public class TipTap:MessageBase{
};
public class NetworkTest : MonoBehaviour {
NetworkClient myClient;
int port = 4545;
float ttimer = 0;
float DCSeconds = 3;
Coroutine CurCor = null;
public float NetPing = 0;
public float NetPing2 = 0;
float pingtime = 0;
bool SendPing = true;
public void StartServer(){
if (CurCor == null) {
CurCor = StartCoroutine (StartServerE());
}
}
public void StartClient(TMP_InputField IPAddress){
if (CurCor == null) {
CurCor = StartCoroutine (StartClientE(IPAddress));
}
}
void Update(){
if (myClient != null) {
if (myClient.isConnected && SendPing) {
pingtime = Time.time;
myClient.Send (MyMessageTypes.Ping,new TipTap());
SendPing = false;
}
}
}
IEnumerator StartServerE(){
ttimer = 0;
NetworkServer.Listen (port);
NetworkServer.RegisterHandler (MsgType.Connect,OnSConnected);
NetworkServer.RegisterHandler (MsgType.Disconnect,OnSDConnected);
NetworkServer.RegisterHandler (MyMessageTypes.Ping,OnPingS);
while (!NetworkServer.active) {
Debug.Log ("Still creating server");
ttimer += Time.deltaTime;
if (ttimer > DCSeconds) {
Debug.Log ("Cant create server");
yield return null;
}
yield return new WaitForEndOfFrame ();
}
Debug.Log ("Server created");
yield return null;
}
IEnumerator StartClientE(TMP_InputField IPAddress){
ttimer = 0;
myClient = new NetworkClient();
myClient.RegisterHandler(MsgType.Connect, OnConnected);
myClient.RegisterHandler(MyMessageTypes.Pong, OnPong);
myClient.Connect(IPAddress.text, port);
while (!myClient.isConnected) {
Debug.Log ("Still connecting to server");
ttimer += Time.deltaTime;
if (ttimer > DCSeconds) {
Debug.Log ("Cant connect to server");
yield return null;
}
yield return new WaitForEndOfFrame ();
}
yield return null;
}
//Client messages
public void OnConnected(NetworkMessage netMsg)
{
Debug.Log("Connected to server");
}
public void OnPong(NetworkMessage netMsg){
NetPing = Mathf.Round(((Time.time - pingtime)*1000)/2);
SendPing = true;
}
//Server messages
public void OnSConnected(NetworkMessage netMsg)
{
Debug.Log("Some one connected to server:"+netMsg.conn.connectionId);
}
public void OnSDConnected(NetworkMessage netMsg)
{
Debug.Log("Some one disconnected from server");
}
public void OnPingS(NetworkMessage netMsg){
NetworkServer.SendToClient (netMsg.conn.connectionId,MyMessageTypes.Pong,new TipTap());
}
}
Am i doing something wrong (i mean i know that this is weak implementation, but still… 30+ its kinda much)?
Maybe i missed some configuration where i can set min latency or something similar?
I hope you will understand what i wrote there =)
Apologize for bad English.
Packets are not send straight away. There is a send delay in the transport in order to compact messages down by using less header space. You can remove this delay with the ConnectionConfig.
My first step would also be to check if it’s specific to Unity. How fast does the terminal command “ping localhost” return? Also, is there anything in the way of your local communication? (A firewall, virus scanner…)
Also, messages are queued. What happens if you directly send a socket communication using winsock?