- Home /
Problems with tcpclient
i have been trying several ways to instantiate a camera when i spawn a player after logging in the server in unity, so far no sucess , i even added the camera to the player prefab but when it spawn it starts to go on and off for some reason heres the code if anyone can help using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using UnityEngine; using Common; using Newtonsoft.Json; using UnityEngine.UI;
namespace Assets.Scripts { public class TcpClientController : MonoBehaviour { [HideInInspector] public Player Player; private Dictionary _playerGameObjectDictionary;
public GameObject PlayerPrefab;
public GameObject ConnectionUi;
public Camera RunCamera;
public Text PlayerNameInput;
public string IpAddress;
public int TcpPort;
public int UdpPort;
void Awake()
{
Player = new Player();
_playerGameObjectDictionary = new Dictionary<Guid, GameObject>();
Player.GameState = GameState.Disconnected;
Player.TcpClient = new TcpClient();
Player.UdpClient = new UdpClient();
}
void Update()
{
if (Player.TcpClient.Connected)
{
switch (Player.GameState)
{
case GameState.Connecting:
Debug.Log("Connecting");
if (Player.TcpClient.GetStream().DataAvailable)
{
Message message = ReceiveMessage();
Player.Id = message.Player.Id;
Player.Name = PlayerNameInput.text;
Debug.Log(message.Title);
Debug.Log(message.Description);
SendMessage(new Message { MessageType = MessageType.PlayerName, Player = Player });
Player.GameState = GameState.Connected;
}
break;
case GameState.Connected:
Debug.Log("Connected");
if (Player.TcpClient.GetStream().DataAvailable)
{
Message message = ReceiveMessage();
Debug.Log(message.Title);
Debug.Log(message.Description);
Player.PlayerInformation = message.Player.PlayerInformation;
Player.GameState = GameState.Syncing;
}
break;
case GameState.Syncing:
Debug.Log("Syncing");
if (Player.TcpClient.GetStream().DataAvailable)
{
Message message = ReceiveMessage();
if (message.MessageType == MessageType.NewPlayer)
{
GameObject playerGameObject = Instantiate(PlayerPrefab, new Vector3(message.Player.PlayerInformation.X, message.Player.PlayerInformation.Y, message.Player.PlayerInformation.Z), Quaternion.identity);
playerGameObject.GetComponent<PlayerUiController>().PlayerName.text = message.Player.Name;
//Camera jogocamera = Instantiate(RunCamera, new Vector3(message.Player.PlayerInformation.X, message.Player.PlayerInformation.Y, message.Player.PlayerInformation.Z), Quaternion.identity);
_playerGameObjectDictionary.Add(message.Player.Id, playerGameObject);
}
else if (message.MessageType == MessageType.FinishedSync)
{
ConnectionUi.SetActive(false);
GameObject playerGameObject = Instantiate(PlayerPrefab, new Vector3(Player.PlayerInformation.X, Player.PlayerInformation.Y, Player.PlayerInformation.Z), Quaternion.identity);
playerGameObject.GetComponent<PlayerController>().TcpClientController = this;
playerGameObject.GetComponent<PlayerController>().Playable = true;
playerGameObject.GetComponent<PlayerController>().enabled =true;
playerGameObject.GetComponent<PlayerUiController>().PlayerName.text = Player.Name;
playerGameObject.GetComponent<Camera>().enabled = false;
_playerGameObjectDictionary.Add(Player.Id, playerGameObject);
Player.GameState = GameState.GameStarted;
}
}
break;
case GameState.GameStarted:
Debug.Log("GameStarted");
if (Player.TcpClient.GetStream().DataAvailable)
{
Message message = ReceiveMessage();
if (message.MessageType == MessageType.Information)
{
Debug.Log(message.Title);
Debug.Log(message.Description);
}
else if (message.MessageType == MessageType.NewPlayer)
{
GameObject playerGameObject = Instantiate(PlayerPrefab, new Vector3(message.Player.PlayerInformation.X, message.Player.PlayerInformation.Y, message.Player.PlayerInformation.Z), Quaternion.identity);
playerGameObject.GetComponent<PlayerUiController>().PlayerName.text = message.Player.Name;
playerGameObject.GetComponent<Camera>().enabled = true;
_playerGameObjectDictionary.Add(message.Player.Id, playerGameObject);
}/*else if (message.MessageType == MessageType.PlayerMovement)
{
if (_playerGameObjectDictionary.ContainsKey(message.Player.Id) && message.Player.Id != Player.Id)
{
_playerGameObjectDictionary[message.Player.Id].transform.position = new Vector3(message.Player.PlayerInformation.X, message.Player.PlayerInformation.Y, message.Player.PlayerInformation.Z);
}
}*/
}
else if (Player.UdpClient.Available > 0)
{
Debug.Log("receiving...");
Message message = ReceiveUdpMessage(Player.IpEndPoint);
if (message.MessageType == MessageType.PlayerMovement)
{
if (_playerGameObjectDictionary.ContainsKey(message.Player.Id) && message.Player.Id != Player.Id)
{
_playerGameObjectDictionary[message.Player.Id].transform.position = new Vector3(message.Player.PlayerInformation.X, message.Player.PlayerInformation.Y, message.Player.PlayerInformation.Z);
}
}
}
break;
}
}
else
{
Debug.Log("Disconnected");
}
}
public void SendMessage(Message message)
{
string messageJson = JsonConvert.SerializeObject(message);
Player.BinaryWriter.Write(messageJson);
}
public Message ReceiveMessage()
{
string messageJson = Player.BinaryReader.ReadString();
Message message = JsonConvert.DeserializeObject<Message>(messageJson);
return message;
}
/*public void SendUdpMessage(Message message)
{
string messageJson = JsonConvert.SerializeObject(message);
int size = Encoding.ASCII.GetByteCount(messageJson);
byte[] messageBytes = new byte[size];
messageBytes = Encoding.ASCII.GetBytes(messageJson);
Player.UdpClient.Send(messageBytes, size, new IPEndPoint(IPAddress.Parse(IpAddress), UdpPort));
}*/
public Message ReceiveUdpMessage(IPEndPoint ipEndPoint)
{
byte[] messageBytes = new byte[65000];
messageBytes = Player.UdpClient.Receive(ref ipEndPoint);
string messageJson = Encoding.ASCII.GetString(messageBytes);
Message message = JsonConvert.DeserializeObject<Message>(messageJson);
return message;
}
public void StartTcpClient()
{
Player.TcpClient.BeginConnect(IPAddress.Parse(IpAddress), TcpPort, AcceptConnection, Player.TcpClient);
Player.GameState = GameState.Connecting;
}
private void AcceptConnection(IAsyncResult asyncResult)
{
TcpClient tcpClient = (TcpClient)asyncResult.AsyncState;
tcpClient.EndConnect(asyncResult);
if (tcpClient.Connected)
{
Debug.Log("Client connected");
Player.BinaryReader = new BinaryReader(tcpClient.GetStream());
Player.BinaryWriter = new BinaryWriter(tcpClient.GetStream());
Player.UdpClient.ExclusiveAddressUse = false;
Player.IpEndPoint = new IPEndPoint(IPAddress.Any, UdpPort);
Player.UdpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
Player.UdpClient.ExclusiveAddressUse = false;
Player.UdpClient.Client.Bind(Player.IpEndPoint);
IPAddress ipAddress = IPAddress.Parse("239.0.0.254");
Player.UdpClient.JoinMulticastGroup(ipAddress);
//Player.UdpClient.Connect(IpAddress, UdpPort);
//Player.UdpClient = new UdpClient();
//Player.IpEndPoint = new IPEndPoint(IPAddress.Parse(IpAddress), UdpPort);
//Player.UdpClient.Connect(Player.IpEndPoint);
}
else
{
Debug.Log("Client connection refused");
}
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612141111im_/https://answers.unity.com/themes/thub/images/avi.jpg)