【发布时间】:2017-08-05 12:50:49
【问题描述】:
所以我正在创建 UNet 多人游戏应用程序,但在接收消息时遇到了问题。
应用程序适用于 android,但我也为计算机构建了它。
当我通过统一编辑器运行Server 并从计算机与客户端连接到它时,我有部分代码来测试连接并发送消息,它正在工作并在播放器连接时发送消息。
当我连接手机时,它不发送消息。
我所知道的是在电话上它返回NetworkTransport.Connect() 值作为1 这意味着它已连接,但我认为它没有连接它,我将在代码后解释它。
这是我的服务器代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.Text;
public class Server : MonoBehaviour
{
private const int MAX_CONNECTION = 100;
private int port = 7777;
private int hostId;
private int webHostId;
private int reliableChannel;
private int unreliableChannel;
private bool isStarted;
private byte error;
private void Start ()
{
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unreliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topology = new HostTopology(cc, MAX_CONNECTION);
hostId = NetworkTransport.AddHost(topology, port, null);
webHostId = NetworkTransport.AddWebsocketHost(topology, port, null);
isStarted = true;
}
private void Update()
{
if(!isStarted) { 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.Nothing:
break;
case NetworkEventType.ConnectEvent:
Debug.Log("Player " + connectionId + " has connected");
break;
case NetworkEventType.DataEvent:
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
Debug.Log("Player " + connectionId + " has sent: " + msg);
break;
case NetworkEventType.DisconnectEvent:
Debug.Log("Player " + connectionId + " has disconnected");
break;
}
}
}
这是客户端代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class Client : MonoBehaviour
{
private const int MAX_CONNECTION = 100;
private int port = 7777;
private int hostId;
private int webHostId;
private int reliableChannel;
private int unreliableChannel;
private int connectionId;
private float connectionTime;
private bool isConnected;
private bool isStarted = false;
private byte error;
private string playerName;
public Text text;
public void Connect()
{
string pName = GameObject.Find("NameInput").GetComponent<InputField>().text;
if(pName == "")
{
Debug.Log("You must enter a name!");
return;
}
playerName = pName;
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unreliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topology = new HostTopology(cc, MAX_CONNECTION);
hostId = NetworkTransport.AddHost(topology, 0);
connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error);
connectionTime = Time.time;
isConnected = true;
}
private void Update()
{
if(!isConnected) { 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.Nothing:
break;
case NetworkEventType.ConnectEvent:
break;
case NetworkEventType.DataEvent:
break;
case NetworkEventType.DisconnectEvent:
break;
}
}
}
这是我如何逐步测试应用程序的。
对于这个测试,我实现了将按钮文本更改为connectionId 的行,即connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error);,它就在该行之后
- 将应用程序构建为
.exe(适用于电脑) - 以
.apk的身份构建和运行应用程序(适用于android)在编辑器中运行应用程序(android 构建)-服务器场景(具有服务器脚本) - 作为客户端在 android 和 pc 上运行应用程序 - 客户端场景(具有客户端脚本)
- 输入名称并在 PC 客户端上按连接 - 统一控制台内弹出
Player 1 has connected - 在 Android 客户端上输入名称并按 Connect - unity 内部没有任何反应,但按钮文本更改为 1(表示已连接)
- 再次按下 PC 客户端上的连接(添加另一个客户端),它应该是播放器 3,因为在 android 连接上返回正值,但它返回
Player 2 has connected
我不知道发生了什么。我需要为 android 连接做些什么,或者可能是因为我正在使用 wifi 连接(它与统一网络管理器正常工作,但现在我无法使用 Lower Api 进行连接)
【问题讨论】:
-
" 它不发送消息" 您的问题的代码中没有发送代码......
-
在
switch下的服务器代码中。它在电脑客户端上做得很好,但在安卓客户端上却不行。当我在统一控制台中与计算机客户端连接时,我收到消息,但是当我使用 android 进行连接时,我没有 -
好的,看起来你的意思是连接事件而不是发送/接收事件。在您的客户端代码中,为什么不将
Debug.Log也放入switch语句中看看会发生什么? -
我现在不明白。我已经在交换机
Debug.Log内的服务器上有了,我检查它是否已连接。 -
@Programmer 我添加了更多关于我的问题的信息
标签: c# android unity3d networking unity5