如何解决Unity 5 MultiPlayer NetworkTransport.Send
所以我是 Unity 的新手,需要制作一个多人游戏,我可以在其中将客户端与游戏服务器上的界面同步,我已经创建了一个客户端和一个服务器,并且客户端确实连接到了服务器,但问题是就是它不能发送消息,服务器接收到的都是空
我将服务器作为应用程序运行,而客户端需要是 WebGL
这是我的 Server.cs :
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class server : MonoBehavIoUr
{
private const int MAX_CONNECTIONS = 2000;
private const string SERVER_IP = "192.168.1.8";
private const int SERVER_PORT = 8999;
private const int SERVER_WEB_PORT = 8998;
private const int BUFFER_SIZE = 400000;
private int reliablechannelid;
private int unreliablechannelid;
private int hostId;
private int webHosted;
private byte[] buffer = new byte[BUFFER_SIZE];
private bool isInit;
// Start is called before the first frame update
void Start()
{
GlobalConfig config = new GlobalConfig();
NetworkTransport.Init(config);
ConnectionConfig cc = new ConnectionConfig();
reliablechannelid = cc.AddChannel(QosType.Reliable);
unreliablechannelid = cc.AddChannel(QosType.Unreliable);
HostTopology topo = new HostTopology(cc,MAX_CONNECTIONS);
hostId = NetworkTransport.AddHost(topo,SERVER_PORT);
webHosted = NetworkTransport.AddWebsocketHost(topo,SERVER_WEB_PORT);
isInit = true;
}
//This function is called when data is sent
void OnData(int hostId,int connectionId,int channelId,byte[] data,int size,NetworkError error)
{
//Here the message being received is deserialized and output to the console
Stream serializedMessage = new MemoryStream(data);
BinaryFormatter formatter = new BinaryFormatter();
string message = formatter.Deserialize(serializedMessage).ToString();
//Output the deserialized message as well as the connection information to the console
Debug.Log("OnData(hostId = " + hostId + ",connectionId = "
+ connectionId + ",channelId = " + channelId + ",data = "
+ message + ",size = " + size + ",error = " + error.ToString() + ")");
Debug.Log("data = " + message);
}
// Update is called once per frame
void Update()
{
{
if (!isInit)
{
return;
}
int outHostId;
int outConnectionId;
int outChannelId;
byte[] buffer = new byte[1024];
int receivedSize;
byte error;
//Set up the Network Transport to receive the incoming message,and decide what type of event
NetworkEventType eventType = NetworkTransport.Receive(out outHostId,out outConnectionId,out outChannelId,buffer,buffer.Length,out receivedSize,out error);
switch (eventType)
{
//Use this case when there is a connection detected
case NetworkEventType.ConnectEvent:
{
//Call the function to deal with the received information
Debug.Log("Connected");
break;
}
//This case is called if the event type is a data event,like the serialized message
case NetworkEventType.DataEvent:
{
//Call the function to deal with the received data
OnData(outHostId,outConnectionId,outChannelId,receivedSize,(NetworkError)error);
break;
}
case NetworkEventType.nothing:
break;
default:
//Output the error
Debug.LogError("UnkNown network message type received: " + eventType);
break;
}
}
}
}
这是我的 Client.cs :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using UnityEngine.UI;
using System.IO;
public class client : MonoBehavIoUr
{
private const int MAX_CONNECTIONS = 2000;
private const string SERVER_IP = "192.168.1.7";
private const int SERVER_PORT = 8999;
private const int SERVER_WEB_PORT = 8998;
private const int BUFFER_SIZE = 400000;
private int connectionId;
private int reliablechannelid;
private int unreliableChannelId;
private int hostId;
bool ok;
private byte error;
// private byte[] buffer = new byte[BUFFER_SIZE];
private bool isConnected;
public string Msg = "test";
public byte[] buffer;
// Start is called before the first frame update
void Connect()
{
GlobalConfig config = new GlobalConfig();
NetworkTransport.Init(config);
ConnectionConfig cc = new ConnectionConfig();
reliablechannelid = cc.AddChannel(QosType.Reliable);
HostTopology topo = new HostTopology(cc,0);
#if UNITY_WEBGL
connectionId = NetworkTransport.Connect(hostId,SERVER_IP,SERVER_WEB_PORT,out error);
Debug.Log((NetworkError)error);
Debug.Log("connectionId= "+connectionId);
#else
connectionId = NetworkTransport.Connect(hostId,SERVER_PORT,out error);
Debug.Log((NetworkError)error);
Debug.Log("connectionId= " + connectionId);
#endif
}
//This is the function that serializes the message before sending it
void SendMyMessage(string textInput)
{
byte error;
byte[] buffer = new byte[1024];
Stream message = new MemoryStream(buffer);
BinaryFormatter formatter = new BinaryFormatter();
//Serialize the message
formatter.Serialize(message,textInput);
//Send the message from the "client" with the serialized message and the connection information
NetworkTransport.Send(hostId,connectionId,reliablechannelid,(int)message.Position,out error);
//If there is an error,output message error to the console
if ((NetworkError)error != NetworkError.Ok)
{
Debug.Log("Message send error: " + (NetworkError)error);
}
}
// Update is called once per frame
void Update()
{
SendMyMessage("heyyyyy");
}
}
解决方法
我找到了解决方案, 我刚刚添加
NetworkTransport.Connect(hostId,SERVER_IP,SERVER_WEB_PORT,out error);
也连接到我的服务器,我不知道服务器也需要连接 希望这会帮助某人
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。