I'm using Unity 5.5.4.
I have create a TCP Server and a Client listener.
Here is how i connect to the server on the Client:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Net.Sockets;
using System.IO;
using System;
using System.Text.RegularExpressions;
using UnityEngine.SceneManagement;
using Newtonsoft.Json;
using System.Linq;
using System.Threading;
public class ClientWorldServer : MonoBehaviour {
public bool socketReady;
public static TcpClient socket;
public static NetworkStream stream;
public static StreamWriter writer;
public static StreamReader reader;
public void ConnectToWorldServer()
{
if (socketReady)
{
return;
}
//Default host and port values;
string host = "127.0.0.1";
int port = 8080;
//ClientLoginServer ClientLoginServer = new ClientLoginServer();
try
{
socket = new TcpClient(host, port);
stream = socket.GetStream();
socket.NoDelay = true;
writer = new StreamWriter(stream);
reader = new StreamReader(stream);
socketReady = true;
//Preserve the connection to worldserver thrue scenes
DontDestroyOnLoad(worldserverConnection);
}
catch (Exception e)
{
Debug.Log("Socket error : " + e.Message);
}
}
}
Inside the same ClientWorldServer
class here is how i check for available data:
// Update is called once per frame
void Update () {
if (socketReady)
{
if (stream.DataAvailable)
{
string data = reader.ReadLine();
if (data != null)
{
OnIncomingData(data);
}
}
}
}
For those who are not familiar with Unity void Update()
is called once per frame. It's pretty much like endless while
loop.
Here is my OnIncomingData
function:
private void OnIncomingData(string data)
{
new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;
JsonData json = JsonConvert.DeserializeObject<JsonData>(data);
string prefix = json.header.Substring(0, 2);
if (prefix != "0x")
{
Debug.Log("Unknown packet: " + data + "\n");
}
else
{
string header = json.header.Substring(2);
int conId = json.connectionId;
//Debug.Log("Header:" + header);
switch (header)
{
default:
Debug.Log("Unknown packet: " + data + "\n");
break;
case "000":
Debug.Log("Empty header received: " + json.header + "\n");
break;
case "001":
if(connectionId == 0)
{
connectionId = conId;
Debug.Log("Connection ID: " + connectionId + "\n");
}
break;
case "004":
DisplayCharacterSelects(json.data, connectionId);
break;
case "005":
PlayerSpawner playerSpawner = new PlayerSpawner();
playerSpawner.SpawnPlayer(json.data, conId);
break;
case "006":
PlayerSpawner OnliePlayersSpawner = new PlayerSpawner();
OnliePlayersSpawner.SpawnOnlinePlayers(json.data);
break;
case "007":
CharacterLogout(json.data);
break;
case "008":
Character character = new Character();
character.updateCharacterPosition(json.data, conId);
break;
case "009":
Character characterR = new Character();
characterR.updateCharacterRotation(json.data, conId);
break;
}
Debug.Log("World Server: " + data);
}
}).Start();
}
On the checks that i've made i can assure you that the server is not slowing the process of sending data. The client is somehow slowing at reading. It's important to say that i'm sending players position when they move to the server and then server responds back to all the clients connected notifying them that some player has changes it's position. That means that a lot of requests are made per 1 second.
Maybe i'm not taking the best approach or i have a rookie mistake but this is my first touch with TCP.
Do you see any reason why processing of the data in many requests can be slowed at the client and if so how can i fix it?