Hi
I am putting together a small prototype using the llapi, so the problem is the connectionID for clients are the same. So I run my server, in the update method, connect event I Debug.Log("Client connected " + connectionId); Just for testing so I know who has connected. But in my client Network.Connect always returns connectionID 1. Here is the code for my server and client, Also remember I am running both Server and 2 Clients on the same machine.
public class ServerNetworkManager : MonoBehaviour
{
#region Fields
public static ServerNetworkManager instance = null;
public delegate void MessageHandler(Message msg);
public static event MessageHandler OnHandleMessage;
// network
private const int MAX_CONNECTIONS = 100;
private int port = 6321;
private int serverID;
private int webHostID;
private int channelID;
private byte error;
private bool isStarted = false;
public List<Player> Players;
// UI
public Text lblConnected;
#endregion
void Awake()
{
if (instance == null) instance = this;
else if (instance != this) Destroy(gameObject);
}
private bool Initialize()
{
NetworkTransport.Init();
var config = new ConnectionConfig();
channelID = config.AddChannel(QosType.ReliableSequenced);
var toplogy = new HostTopology(config, MAX_CONNECTIONS);
serverID = NetworkTransport.AddHost(toplogy, port, null);
webHostID = NetworkTransport.AddWebsocketHost(toplogy, port, null);
isStarted = true;
Debug.Log("Sever started on port " + port);
DontDestroyOnLoad(gameObject);
Players = new List<Player>();
return true;
}
void Start()
{
Initialize();
lblConnected = GameObject.Find("lblConnectedClients").GetComponent<Text>();
lblConnected.text = "Connected: 0";
}
void Update()
{
if (isStarted == false)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData)
{
case NetworkEventType.ConnectEvent:
Debug.Log("Client connected " + connectionId);
AddPlayer(connectionId);
break;
case NetworkEventType.DataEvent:
string msg = Encoding.Unicode.GetString(recBuffer, 0, bufferSize);
var message = JsonUtility.FromJson<Message>(msg);
OnHandleMessage(message);
break;
case NetworkEventType.DisconnectEvent:
var player = Players.First(p => p.ConnectionID == connectionId);
Players.Remove(player);
UpdateConnected();
break;
}
}
}
Here is the client code :
public class ClientNetworkManager : MonoBehaviour
{
public static ClientNetworkManager instance = null;
public delegate void MessageHandler(Message msg);
public static event MessageHandler OnHandleMessage;
// network
private const int MAX_CONNECTIONS = 100;
private int _port = 6321;
private int _serverID;
private int _channelID;
private byte _error;
public int ConnectionID { get; set; }
// UI
public Text lblStatus;
void Awake()
{
if (instance == null) instance = this;
else if (instance != this) Destroy(gameObject);
lblStatus = GameObject.Find("lblStatus").GetComponent<Text>();
lblStatus.text = "Status: Not Connected";
}
void Start()
{
NetworkTransport.Init();
}
void Update()
{
if (ConnectionID == 0)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData)
{
case NetworkEventType.DataEvent:
string msg = Encoding.Unicode.GetString(recBuffer, 0, bufferSize);
var message = JsonUtility.FromJson<Message>(msg);
OnHandleMessage(message);
break;
}
}
public void Connect()
{
if (ConnectionID > 0)
return;
var config = new ConnectionConfig();
_channelID = config.AddChannel(QosType.ReliableSequenced);
var toplogy = new HostTopology(config, MAX_CONNECTIONS);
_serverID = NetworkTransport.AddHost(toplogy, _port, null);
ConnectionID = NetworkTransport.Connect(_serverID, "127.0.0.1", _port, 0, out _error);
Debug.Log(ConnectionID);
lblStatus.text = "Status: Connected - " + ConnectionID.ToString();
}
public void SendMessageToServer(Message message)
{
var json = JsonUtility.ToJson(message);
byte[] msg = Encoding.Unicode.GetBytes(json);
NetworkTransport.Send(_serverID, ConnectionID, _channelID, msg, json.Length * sizeof(char), out _error);
}
}
I think I have been looking at this code for to long and I cant see my mistake.
Thanks for helping.