您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
2.3 KiB
2.3 KiB
using UnityEngine;
using UnityEngine.Assertions;
using Unity.Collections;
using Unity.Networking.Transport;
public class ServerBehaviour : MonoBehaviour
{
public NetworkDriver m_Driver;
private NativeList<NetworkConnection> m_Connections;
void Start ()
{
m_Driver = NetworkDriver.Create();
var endpoint = NetworkEndPoint.AnyIpv4;
endpoint.Port = 9000;
if (m_Driver.Bind(endpoint) != 0)
Debug.Log("Failed to bind to port 9000");
else
m_Driver.Listen();
m_Connections = new NativeList<NetworkConnection>(16, Allocator.Persistent);
}
public void OnDestroy()
{
m_Driver.Dispose();
m_Connections.Dispose();
}
void Update ()
{
m_Driver.ScheduleUpdate().Complete();
// CleanUpConnections
for (int i = 0; i < m_Connections.Length; i++)
{
if (!m_Connections[i].IsCreated)
{
m_Connections.RemoveAtSwapBack(i);
--i;
}
}
// AcceptNewConnections
NetworkConnection c;
while ((c = m_Driver.Accept()) != default(NetworkConnection))
{
m_Connections.Add(c);
Debug.Log("Accepted a connection");
}
DataStreamReader stream;
for (int i = 0; i < m_Connections.Length; i++)
{
Assert.IsTrue(m_Connections[i].IsCreated);
NetworkEvent.Type cmd;
while ((cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream)) != NetworkEvent.Type.Empty)
{
if (cmd == NetworkEvent.Type.Data)
{
uint number = stream.ReadUInt();
Debug.Log("Got " + number + " from the Client adding + 2 to it.");
number +=2;
var writer = m_Driver.BeginSend(NetworkPipeline.Null, m_Connections[i]);
writer.WriteUInt(number);
m_Driver.EndSend(writer);
}
else if (cmd == NetworkEvent.Type.Disconnect)
{
Debug.Log("Client disconnected from server");
m_Connections[i] = default(NetworkConnection);
}
}
}
}
}