我正在尝试使用Unitys传输api建立网络通信。
网络连接正在运行,两个设备都可以通过 NetworkDiscovery 组件相互发送和接收广播。到目前为止一切顺利。
现在,如果我尝试通过Transport API发送数据,则我在发送设备上收到错误,而在接收方则什么也没有。
public void Send()
{
Connection = new NetworkConnection();
var info = LobbyRenderer.GetSelectedHostInfo();
Connection.Initialize(info.ipv4, 1, 1, new HostTopology(new ConnectionConfig(), 1));
byte connectionError;
websocketId = NetworkTransport.AddWebsocketHost(new HostTopology(new ConnectionConfig(), 1), 47774);
connectionId = NetworkTransport.Connect(websocketId, info.ipv4, 47774, 0, out connectionError);
byte sendError;
byte[] bytes = new byte[2] { 1,1 };
NetworkTransport.Send(websocketId, connectionId, 1, bytes, 2, out sendError);
}
connectionError字节返回4, sendError字节返回2。
Unity控制台:
我很感谢有关如何进行此工作的提示。
答案 0 :(得分:1)
我不确定是否是这种情况,但是我在您的代码中看不到任何QOS通道,请尝试添加一个QOS通道,发送时还必须提供QOS通道。
public void Send()
{
Connection = new NetworkConnection();
var info = LobbyRenderer.GetSelectedHostInfo();
Connection.Initialize(info.ipv4, 1, 1, new HostTopology(new ConnectionConfig(), 1));
byte connectionError;
ConnectionConfig config = new ConnectionConfig();
int myReliableChannelId = config.AddChannel(QosType.Reliable);
int myUnreliableChannelId = config.AddChannel(QosType.Unreliable);
websocketId = NetworkTransport.AddWebsocketHost(new HostTopology(config, 1), 47774);
connectionId = NetworkTransport.Connect(websocketId, info.ipv4, 47774, 0, out connectionError);
byte sendError;
byte[] bytes = new byte[2] { 1,1 };
NetworkTransport.Send(websocketId, connectionId, myReliableChannelId, bytes, 2, out sendError);
}