我正在编写一个多人游戏,其中手机上的多个客户端应该能够连接到基于 PC 的服务器。我的编辑器测试一切正常,编辑器客户端连接正常,我现在已经将客户端项目构建到我的手机上,当我按下加入服务器时没有任何反应。
服务器创建方法:
public void StartServer()
{
DontDestroyOnLoad(gameObject);
Debug.Log("Started");
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
Debug.Log(Dns.GetHostName());
foreach (IPAddress addr in localIPs)
{
if (addr.AddressFamily == AddressFamily.InterNetwork)
{
ip = addr;
}
}
var config = new NetPeerConfiguration("TEC") { Port = 7777, LocalAddress = ip } ;
config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
server = new NetServer(config);
server.Start();
Debug.Log("Server Started");
createButton.gameObject.SetActive(false);
serverInfoGroup.SetActive(true);
ipSlot.gameObject.SetActive(true);
playerTextSlotsGroup.SetActive(true);
foreach (var playerSlot in playerTextSlots)
{
playerSlot.gameObject.SetActive(false);
}
ipSlot.text = Convert.ToString(ip);
客户加入方式:
public void JoinServer()
{
string ip = ipBox.text;
userName = userBox.text;
var config = new NetPeerConfiguration("TEC");
Debug.Log("Connecting with " + ip + " and " + userName);
client = new NetClient(config);
client.RegisterReceivedCallback(new SendOrPostCallback(GotMessage));
client.Start();
NetOutgoingMessage approval = client.CreateMessage();
approval.Write("dunno");
client.Connect(ip, 7777, approval);
attemptedConnection = true;
Debug.Log("Server joined");
}
当客户端点击回车按钮时应该调用 JoinServer,两个输入框是我输入 IP 和用户名的地方。我一直在使用我的电脑的IPv4地址,从编辑器可以加入的服务器创建方法中获得,但我的手机没有构建。
我现在已经在安卓模拟器上测试了这个程序,但是同样的事情发生了,对连接服务器按钮没有反应。