Hi there,
i’m working on my custom networking system using the LLAPI, and so far i was able to open sockets between a server and a client so i can send messages from client which can be received on server.
While the server log tells me that he’s receiving messages (so i can control a character from client to server), the client log tells me that there is an incoming connection (which of course i presume is from server) but i can’t find a way to transmit from server and receive what server sent on clients.
Here’s what i got:
using UnityEngine;
using UnityEngine.Networking;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Globalization;
public class PlayerNetworkSync : NetworkBehaviour
{
HostTopology topology;
int hostId, connectionId;
int myReiliableChannelId;
int myUnreliableChannelId;
//[SyncVar]
public Vector3 syncPos;
//[SyncVar]
private Vector3 targetPosition;
//[SerializeField]
Transform playerTransform;
//[SerializeField]
float syncRate = 10;
// Use this for initialization
void Start()
{
this.tag = "DataSynchro";
playerTransform = transform;
// Initializing the Transport Layer with no arguments (default settings)
NetworkTransport.Init();
ConnectionConfig config = new ConnectionConfig();
myReiliableChannelId = config.AddChannel(QosType.Reliable); //lento ma verifica il dispatch
myUnreliableChannelId = config.AddChannel(QosType.Unreliable); //veloce ma non verificaa il dispatch
topology = new HostTopology(config, 3);
}
bool hostStarted;
bool hostConnected;
bool isClient = false;
string buttonHostText = "Start Socket";
string buttonClientText = "Connect To Host";
void OnGUI()
{
//start socket
if (GUI.Button(new Rect(300, 150, 150, 50), buttonHostText))
{
if (!hostStarted)
{
hostId = NetworkTransport.AddHost(topology, 7777);
Debug.LogError("HostId is : " + hostId.ToString());
hostStarted = true;
buttonHostText = "HostID: " + hostId.ToString() + " - Stop Host";
}
else
{
hostStarted = false;
buttonHostText = "Start Socket";
}
}
if (GUI.Button(new Rect(300, 250, 150, 50), buttonClientText))
{
byte error;
if (!hostConnected)
{
//opens a socket on client
int chostId = NetworkTransport.AddHost(topology, (int)Random.Range(1111, 9999));
//connect to server
connectionId = NetworkTransport.Connect(0, "127.0.0.1", 7777, 0, out error);
if (connectionId != 0)
{
Debug.LogError("HostID : " + chostId + " ConnID : " + connectionId.ToString());
hostConnected = true;
isClient = true;
buttonHostText = "HostID : " + chostId + " ConnID : " + connectionId.ToString();
buttonClientText = "Disconnect";
}else
Debug.LogError("Connection Failed");
}else
{
bool result = NetworkTransport.Disconnect(0, connectionId, out error);
Debug.LogError("Disconnection is : " + result.ToString());
hostConnected = false;
buttonClientText = "Connect To Host";
}
}
}
// Update is called once per frame
public float speed = 200f;
float timeToUpdate = 0;
void Update()
{
if (!hostConnected && !hostStarted)
return;
TransmitPosition();
SyncPosition();
}
void SyncPosition()
{
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
//turns out that server is receiving messages from client, but client receives only an incoming connection
//from server, and no messages
NetworkEventType recNetworkEvent = NetworkTransport.ReceiveFromHost(hostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recNetworkEvent)
{
case NetworkEventType.Nothing:
break;
case NetworkEventType.ConnectEvent:
Debug.LogError("incoming connection event received");
break;
case NetworkEventType.DataEvent:
Stream stream = new MemoryStream(recBuffer);
BinaryFormatter formatter = new BinaryFormatter();
string message = formatter.Deserialize(stream) as string;
Debug.LogError("incoming message event received: " + message);
timeToUpdate += Time.deltaTime;
transform.position = new Vector3((float.Parse(message, CultureInfo.InvariantCulture)), float.Parse(message, CultureInfo.InvariantCulture), 1);
timeToUpdate = 0;
break;
case NetworkEventType.DisconnectEvent:
Debug.LogError("remote client event disconnected");
break;
}
}
void FixedUpdate()
{
}
void TransmitPosition()
{
byte error;
byte[] buffer = new byte[1024];
Stream stream = new MemoryStream(buffer);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, transform.position.x.ToString());
int bufferSize = 1024;
//this for sure sends from client on server, but he's also sending from server to client?
NetworkTransport.Send(hostId, connectionId, myReiliableChannelId, buffer, bufferSize, out error);
}
}
My class is very simple so far. You can copy and paste on a GameObject script. You can build this and see there’s two gui buttons (server/client behavior). I set a Debug.LogError to see what’s happening on the build too.
Run two instances of the build on the same machine, one as server and one as client.
You will see that the server is getting right messages while the client don’t.
I suppose that if i want to use LLAPI to build my custom networking multiplayer system, i’d like to have:
- Code for dedicated server
- Code for local client connected to local server
- Code for remote clients
From that, i will able to build a room to host other clients, keep in mind that i need a simple two players networking system, nothing more. But i can’t use HLAPI because i really need to build my own messages system, due to my game’s nature.
Thank you so much.